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