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 33 namespace OHOS { 34 // Check all dependencies's availability before the timeout period ended, [200, 60000]. 35 const int32_t MIN_DEPENDENCY_TIMEOUT = 200; 36 const int32_t MAX_DEPENDENCY_TIMEOUT = 60000; 37 const int32_t DEFAULT_DEPENDENCY_TIMEOUT = 6000; 38 39 class LocalAbilityManager : public LocalAbilityManagerStub { 40 DECLARE_SINGLE_INSTANCE_BASE(LocalAbilityManager); 41 42 public: 43 bool AddAbility(SystemAbility* ability); 44 bool RemoveAbility(int32_t systemAbilityId); 45 SystemAbility* GetAbility(int32_t systemAbilityId); 46 bool GetRunningStatus(int32_t systemAbilityId); 47 bool AddSystemAbilityListener(int32_t systemAbilityId, int32_t listenerSaId); 48 bool RemoveSystemAbilityListener(int32_t systemAbilityId, int32_t listenerSaId); 49 std::vector<int32_t> CheckDependencyStatus(const std::vector<int32_t>& dependSas); 50 void StartSystemAbilityTask(SystemAbility* sa); 51 bool CheckSystemAbilityManagerReady(); 52 bool InitSystemAbilityProfiles(const std::string& profilePath, int32_t saId); 53 void ClearResource(); 54 void StartOndemandSystemAbility(int32_t systemAbilityId); 55 void StopOndemandSystemAbility(int32_t systemAbilityId); 56 bool StartAbility(int32_t systemAbilityId, const std::string& eventStr) override; 57 bool ActiveAbility(int32_t systemAbilityId, 58 const nlohmann::json& activeReason) override; 59 bool IdleAbility(int32_t systemAbilityId, const nlohmann::json& idleReason, 60 int32_t& delayTime) override; 61 bool StopAbility(int32_t systemAbilityId, const std::string& eventStr) override; 62 void DoStartSAProcess(const std::string& profilePath, int32_t saId); 63 void SetUpdateList(const std::string& updateList); 64 void SetStartReason(int32_t systemAbilityId, const nlohmann::json& event); 65 void SetStopReason(int32_t systemAbilityId, const nlohmann::json& event); 66 nlohmann::json& GetStartReason(int32_t systemAbilityId); 67 nlohmann::json& GetStopReason(int32_t systemAbilityId); 68 SystemAbilityOnDemandReason JsonToOnDemandReason(const nlohmann::json& reasonJson); 69 bool SendStrategyToSA(int32_t type, int32_t systemAbilityId, int32_t level, std::string& action) override; 70 71 private: 72 LocalAbilityManager(); 73 ~LocalAbilityManager() = default; 74 75 bool AddLocalAbilityManager(); 76 void RegisterOnDemandSystemAbility(int32_t saId); 77 void FindAndStartPhaseTasks(int32_t saId); 78 void StartPhaseTasks(const std::list<SystemAbility*>& startTasks); 79 void CheckTrustSa(const std::string& path, const std::string& process, const std::list<SaProfile>& saInfos); 80 sptr<ISystemAbilityStatusChange> GetSystemAbilityStatusChange(); 81 void FindAndNotifyAbilityListeners(int32_t systemAbilityId, const std::string& deviceId, int32_t code); 82 void NotifyAbilityListener(int32_t systemAbilityId, int32_t listenerSaId, 83 const std::string& deviceId, int32_t code); 84 void WaitForTasks(); 85 void StartDependSaTask(SystemAbility* ability); 86 class SystemAbilityListener : public SystemAbilityStatusChangeStub { 87 public: 88 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 89 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 90 }; 91 92 bool CheckAndGetProfilePath(const std::string& profilePath, std::string& realProfilePath); 93 bool InitializeSaProfiles(int32_t saId); 94 bool InitializeOnDemandSaProfile(int32_t saId); 95 bool InitializeRunOnCreateSaProfiles(uint32_t bootPhase); 96 bool InitializeSaProfilesInnerLocked(const SaProfile& saProfile); 97 bool Run(int32_t saId); 98 bool NeedRegisterOnDemand(const SaProfile& saProfile, int32_t saId); 99 bool OnStartAbility(int32_t systemAbilityId); 100 bool OnStopAbility(int32_t systemAbilityId); 101 std::string GetTraceTag(const std::string& profilePath); 102 103 std::map<int32_t, SystemAbility*> abilityMap_; 104 std::map<uint32_t, std::list<SystemAbility*>> abilityPhaseMap_; 105 std::shared_mutex abilityMapLock_; 106 sptr<LocalAbilityManager> localAbilityManager_; 107 std::map<int32_t, nlohmann::json> saIdToStartReason_; 108 std::map<int32_t, nlohmann::json> saIdToStopReason_; 109 // Max task number in pool is 20. 110 const int32_t MAX_TASK_NUMBER = 20; 111 // Check dependent sa status every 50 ms, it equals 50000us. 112 const int32_t CHECK_DEPENDENT_SA_PERIOD = 50000; 113 114 sptr<ISystemAbilityStatusChange> statusChangeListener_; 115 std::map<int32_t, std::list<int32_t>> listenerMap_; 116 std::mutex ReasonLock_; 117 std::mutex listenerLock_; 118 std::shared_ptr<ParseUtil> profileParser_; 119 120 std::condition_variable startPhaseCV_; 121 std::mutex startPhaseLock_; 122 int32_t startTaskNum_ = 0; 123 std::u16string procName_; 124 int64_t startBegin_ = 0; 125 126 // Thread pool used to start system abilities in parallel. 127 std::unique_ptr<ThreadPool> initPool_; 128 }; 129 } 130 #endif 131