• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FfrtStatCmdProc(int32_t fd, int32_t cmd) override;
78     bool FfrtDumperProc(std::string& result) override;
79     int32_t SystemAbilityExtProc(const std::string& extension, int32_t said,
80         SystemAbilityExtensionPara* callback, bool isAsync = false) override;
81     int32_t ServiceControlCmd(int32_t fd, int32_t systemAbilityId, const std::vector<std::u16string>& args) override;
82     void IdentifyUnusedResident();
83 
84 private:
85     LocalAbilityManager();
86     ~LocalAbilityManager();
87 
88     bool AddLocalAbilityManager();
89     void RegisterOnDemandSystemAbility(int32_t saId);
90     void FindAndStartPhaseTasks(int32_t saId);
91     void StartPhaseTasks(const std::list<SystemAbility*>& startTasks);
92     void CheckTrustSa(const std::string& path, const std::string& process, const std::list<SaProfile>& saInfos);
93     sptr<ISystemAbilityStatusChange> GetSystemAbilityStatusChange();
94     void FindAndNotifyAbilityListeners(int32_t systemAbilityId, const std::string& deviceId, int32_t code);
95     void NotifyAbilityListener(int32_t systemAbilityId, int32_t listenerSaId,
96         const std::string& deviceId, int32_t code);
97     void WaitForTasks();
98     void StartDependSaTask(SystemAbility* ability);
99     class SystemAbilityListener : public SystemAbilityStatusChangeStub {
100     public:
101         void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
102         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
103     };
104 
105     bool CheckAndGetProfilePath(const std::string& profilePath, std::string& realProfilePath);
106     bool InitializeSaProfiles(int32_t saId);
107     bool InitializeOnDemandSaProfile(int32_t saId);
108     bool InitializeRunOnCreateSaProfiles(uint32_t bootPhase);
109     bool InitializeSaProfilesInnerLocked(const SaProfile& saProfile);
110     bool Run(int32_t saId);
111     bool NeedRegisterOnDemand(const SaProfile& saProfile, int32_t saId);
112     bool OnStartAbility(int32_t systemAbilityId);
113     bool OnStopAbility(int32_t systemAbilityId);
114     std::string GetTraceTag(const std::string& profilePath);
115     bool IsResident();
116     bool IsConfigUnused();
117     void InitUnusedCfg();
118     void StartTimedQuery();
119     void StopTimedQuery();
120     void IdentifyUnusedOndemand();
121     bool NoNeedCheckUnused(int32_t saId);
122     void LimitUnusedTimeout(int32_t saId, int32_t timeout);
123     bool GetSaLastRequestTime(int32_t saId, uint64_t& lastRequestTime);
124     void StartResidentTimer();
125     void StartOnDemandTimer();
126 
127     std::map<int32_t, SystemAbility*> localAbilityMap_;
128     std::map<uint32_t, std::list<SystemAbility*>> abilityPhaseMap_;
129     std::shared_mutex localAbilityMapLock_;
130     sptr<LocalAbilityManager> localAbilityManager_;
131     std::map<int32_t, nlohmann::json> saIdToStartReason_;
132     std::map<int32_t, nlohmann::json> saIdToStopReason_;
133     // Max task number in pool is 20.
134     const int32_t MAX_TASK_NUMBER = 20;
135     // Check dependent sa status every 50 ms, it equals 50000us.
136     const int32_t CHECK_DEPENDENT_SA_PERIOD = 50000;
137 
138     std::mutex listenerLock_;
139     sptr<ISystemAbilityStatusChange> statusChangeListener_;
140     std::map<int32_t, std::list<std::pair<int32_t, ListenerState>>> localListenerMap_;
141     std::mutex ReasonLock_;
142     std::shared_ptr<ParseUtil> profileParser_;
143 
144     std::condition_variable startPhaseCV_;
145     std::mutex startPhaseLock_;
146     int32_t startTaskNum_ = 0;
147     std::u16string procName_;
148     int64_t startBegin_ = 0;
149 
150     // Thread pool used to start system abilities in parallel.
151     std::unique_ptr<ThreadPool> initPool_;
152     std::unique_ptr<Utils::Timer> idleTimer_;
153     // longtime-unusedtimeout map
154     std::shared_mutex unusedCfgMapLock_;
155     std::map<int32_t, int32_t> unusedCfgMap_;
156     uint32_t ondemandTimer_ = 0;
157     timer_t residentTimer_ = nullptr;
158 };
159 }
160 #endif
161