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 70 private: 71 LocalAbilityManager(); 72 ~LocalAbilityManager() = default; 73 74 bool AddLocalAbilityManager(); 75 void RegisterOnDemandSystemAbility(int32_t saId); 76 void FindAndStartPhaseTasks(int32_t saId); 77 void StartPhaseTasks(const std::list<SystemAbility*>& startTasks); 78 void CheckTrustSa(const std::string& path, const std::string& process, const std::list<SaProfile>& saInfos); 79 sptr<ISystemAbilityStatusChange> GetSystemAbilityStatusChange(); 80 void FindAndNotifyAbilityListeners(int32_t systemAbilityId, const std::string& deviceId, int32_t code); 81 void NotifyAbilityListener(int32_t systemAbilityId, int32_t listenerSaId, 82 const std::string& deviceId, int32_t code); 83 void WaitForTasks(); 84 void StartDependSaTask(SystemAbility* ability); 85 class SystemAbilityListener : public SystemAbilityStatusChangeStub { 86 public: 87 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 88 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 89 }; 90 91 bool CheckAndGetProfilePath(const std::string& profilePath, std::string& realProfilePath); 92 bool InitializeSaProfiles(int32_t saId); 93 bool InitializeOnDemandSaProfile(int32_t saId); 94 bool InitializeRunOnCreateSaProfiles(uint32_t bootPhase); 95 bool InitializeSaProfilesInnerLocked(const SaProfile& saProfile); 96 bool Run(int32_t saId); 97 bool NeedRegisterOnDemand(const SaProfile& saProfile, int32_t saId); 98 bool OnStartAbility(int32_t systemAbilityId); 99 bool OnStopAbility(int32_t systemAbilityId); 100 std::string GetTraceTag(const std::string& profilePath); 101 102 std::map<int32_t, SystemAbility*> abilityMap_; 103 std::map<uint32_t, std::list<SystemAbility*>> abilityPhaseMap_; 104 std::shared_mutex abilityMapLock_; 105 sptr<LocalAbilityManager> localAbilityManager_; 106 std::map<int32_t, nlohmann::json> saIdToStartReason_; 107 std::map<int32_t, nlohmann::json> saIdToStopReason_; 108 // Max task number in pool is 20. 109 const int32_t MAX_TASK_NUMBER = 20; 110 // Check dependent sa status every 50 ms, it equals 50000us. 111 const int32_t CHECK_DEPENDENT_SA_PERIOD = 50000; 112 113 sptr<ISystemAbilityStatusChange> statusChangeListener_; 114 std::map<int32_t, std::list<int32_t>> listenerMap_; 115 std::mutex ReasonLock_; 116 std::mutex listenerLock_; 117 std::shared_ptr<ParseUtil> profileParser_; 118 119 std::condition_variable startPhaseCV_; 120 std::mutex startPhaseLock_; 121 int32_t startTaskNum_ = 0; 122 std::u16string procName_; 123 int64_t startBegin_ = 0; 124 125 // Thread pool used to start system abilities in parallel. 126 std::unique_ptr<ThreadPool> initPool_; 127 }; 128 } 129 #endif 130