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