1 /*
2 * Copyright (c) 2022 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 #include "module_running_record.h"
17 #include "app_mgr_service_inner.h"
18 #include "app_running_record.h"
19 #include "hilog_wrapper.h"
20 #include "hitrace_meter.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
24 namespace {
25 const std::string ABILITY_OWNER_USERID = "AbilityMS_Owner_UserId";
26 }
ModuleRunningRecord(const std::shared_ptr<ApplicationInfo> & info,const std::shared_ptr<AMSEventHandler> & eventHandler)27 ModuleRunningRecord::ModuleRunningRecord(
28 const std::shared_ptr<ApplicationInfo> &info, const std::shared_ptr<AMSEventHandler> &eventHandler)
29 : appInfo_(info), eventHandler_(eventHandler)
30 {}
31
~ModuleRunningRecord()32 ModuleRunningRecord::~ModuleRunningRecord()
33 {}
34
Init(const HapModuleInfo & info)35 void ModuleRunningRecord::Init(const HapModuleInfo &info)
36 {
37 owenInfo_ = info;
38 owenState_ = ModuleRecordState::INITIALIZED_STATE;
39 }
40
GetModuleName() const41 const std::string &ModuleRunningRecord::GetModuleName() const
42 {
43 return owenInfo_.moduleName;
44 }
45
GetAppInfo()46 const std::shared_ptr<ApplicationInfo> ModuleRunningRecord::GetAppInfo()
47 {
48 return appInfo_;
49 }
50
GetAbilityRunningRecordByToken(const sptr<IRemoteObject> & token) const51 std::shared_ptr<AbilityRunningRecord> ModuleRunningRecord::GetAbilityRunningRecordByToken(
52 const sptr<IRemoteObject> &token) const
53 {
54 if (!token) {
55 HILOG_ERROR("token is null");
56 return nullptr;
57 }
58 const auto &iter = abilities_.find(token);
59 if (iter != abilities_.end()) {
60 return iter->second;
61 }
62 return nullptr;
63 }
64
AddAbility(const sptr<IRemoteObject> & token,const std::shared_ptr<AbilityInfo> & abilityInfo,const std::shared_ptr<AAFwk::Want> & want)65 std::shared_ptr<AbilityRunningRecord> ModuleRunningRecord::AddAbility(const sptr<IRemoteObject> &token,
66 const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<AAFwk::Want> &want)
67 {
68 HILOG_INFO("Add ability.");
69 if (!token || !abilityInfo) {
70 HILOG_ERROR("Param abilityInfo or token is null");
71 return nullptr;
72 }
73 if (GetAbilityRunningRecordByToken(token)) {
74 HILOG_ERROR("AbilityRecord already exists and no need to add");
75 return nullptr;
76 }
77 auto abilityRecord = std::make_shared<AbilityRunningRecord>(abilityInfo, token);
78 abilityRecord->SetWant(want);
79 if (appInfo_) {
80 abilityRecord->SetIsSingleUser(appInfo_->singleton);
81 }
82 if (want) {
83 abilityRecord->SetOwnerUserId(want->GetIntParam(ABILITY_OWNER_USERID, -1));
84 }
85 abilities_.emplace(token, abilityRecord);
86 return abilityRecord;
87 }
88
IsLastAbilityRecord(const sptr<IRemoteObject> & token)89 bool ModuleRunningRecord::IsLastAbilityRecord(const sptr<IRemoteObject> &token)
90 {
91 if (!token) {
92 HILOG_ERROR("token is nullptr");
93 return false;
94 }
95
96 return ((abilities_.size() == 1) && (abilities_.find(token) != abilities_.end()));
97 }
98
GetPageAbilitySize()99 int32_t ModuleRunningRecord::GetPageAbilitySize()
100 {
101 int pageAbilitySize = 0;
102 for (auto it : abilities_) {
103 std::shared_ptr<AbilityRunningRecord> abilityRunningRecord = it.second;
104 std::shared_ptr<AbilityInfo> abilityInfo = abilityRunningRecord->GetAbilityInfo();
105 if (abilityInfo->type == AbilityType::PAGE) {
106 pageAbilitySize++;
107 }
108 }
109
110 return pageAbilitySize;
111 }
112
GetAbilities() const113 const std::map<const sptr<IRemoteObject>, std::shared_ptr<AbilityRunningRecord>> &ModuleRunningRecord::GetAbilities()
114 const
115 {
116 return abilities_;
117 }
118
GetAbilityByTerminateLists(const sptr<IRemoteObject> & token) const119 std::shared_ptr<AbilityRunningRecord> ModuleRunningRecord::GetAbilityByTerminateLists(
120 const sptr<IRemoteObject> &token) const
121 {
122 if (!token) {
123 HILOG_ERROR("GetAbilityByTerminateLists error, token is null");
124 return nullptr;
125 }
126 const auto &iter = terminateAbilities_.find(token);
127 if (iter != terminateAbilities_.end()) {
128 return iter->second;
129 }
130 return nullptr;
131 }
132
GetAbilityRunningRecord(const int64_t eventId) const133 std::shared_ptr<AbilityRunningRecord> ModuleRunningRecord::GetAbilityRunningRecord(const int64_t eventId) const
134 {
135 HILOG_INFO("Get ability running record by eventId.");
136 const auto &iter = std::find_if(abilities_.begin(), abilities_.end(), [eventId](const auto &pair) {
137 return pair.second->GetEventId() == eventId;
138 });
139 if (iter != abilities_.end()) {
140 return iter->second;
141 }
142
143 const auto &finder = std::find_if(terminateAbilities_.begin(),
144 terminateAbilities_.end(),
145 [eventId](const auto &pair) { return pair.second->GetEventId() == eventId; });
146 if (finder != terminateAbilities_.end()) {
147 return finder->second;
148 }
149 return nullptr;
150 }
151
OnAbilityStateChanged(const std::shared_ptr<AbilityRunningRecord> & ability,const AbilityState state)152 void ModuleRunningRecord::OnAbilityStateChanged(
153 const std::shared_ptr<AbilityRunningRecord> &ability, const AbilityState state)
154 {
155 if (!ability) {
156 HILOG_ERROR("ability is null");
157 return;
158 }
159 AbilityState oldState = ability->GetState();
160 ability->SetState(state);
161 HILOG_INFO("Ability state change from %{public}d to %{public}d, name is %{public}s.",
162 oldState, state, ability->GetName().c_str());
163 auto serviceInner = appMgrServiceInner_.lock();
164 if (serviceInner) {
165 serviceInner->OnAbilityStateChanged(ability, state);
166 }
167 }
168
LaunchAbility(const std::shared_ptr<AbilityRunningRecord> & ability)169 void ModuleRunningRecord::LaunchAbility(const std::shared_ptr<AbilityRunningRecord> &ability)
170 {
171 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
172 HILOG_INFO("Launch ability.");
173 if (!ability || !ability->GetToken()) {
174 HILOG_ERROR("null abilityRecord or abilityToken");
175 return;
176 }
177 const auto &iter = abilities_.find(ability->GetToken());
178 if (iter != abilities_.end() && appLifeCycleDeal_->GetApplicationClient()) {
179 HILOG_INFO("Schedule launch ability, name is %{public}s.", ability->GetName().c_str());
180 appLifeCycleDeal_->LaunchAbility(ability);
181 ability->SetState(AbilityState::ABILITY_STATE_READY);
182 } else {
183 HILOG_ERROR("Can not find ability or get appThread.");
184 }
185 }
186
LaunchPendingAbilities()187 void ModuleRunningRecord::LaunchPendingAbilities()
188 {
189 HILOG_INFO("Launch pending abilities.");
190
191 if (abilities_.empty()) {
192 HILOG_ERROR("abilities_ is empty");
193 return;
194 }
195
196 for (auto item : abilities_) {
197 HILOG_INFO("state : %{public}d", item.second->GetState());
198 if (item.second->GetState() == AbilityState::ABILITY_STATE_CREATE) {
199 LaunchAbility(item.second);
200 }
201 }
202 }
203
TerminateAbility(const std::shared_ptr<AppRunningRecord> & appRecord,const sptr<IRemoteObject> & token,const bool isForce)204 void ModuleRunningRecord::TerminateAbility(const std::shared_ptr<AppRunningRecord> &appRecord,
205 const sptr<IRemoteObject> &token, const bool isForce)
206 {
207 HILOG_INFO("Terminate ability.");
208 auto abilityRecord = GetAbilityRunningRecordByToken(token);
209 if (!abilityRecord) {
210 HILOG_ERROR("abilityRecord is nullptr");
211 return;
212 }
213
214 terminateAbilities_.emplace(token, abilityRecord);
215 abilities_.erase(token);
216
217 if (!isForce) {
218 auto curAbilityState = abilityRecord->GetState();
219 auto curAbilityType = abilityRecord->GetAbilityInfo()->type;
220 if (curAbilityState != AbilityState::ABILITY_STATE_BACKGROUND &&
221 curAbilityType == AppExecFwk::AbilityType::PAGE) {
222 HILOG_ERROR("current state(%{public}d) error", static_cast<int32_t>(curAbilityState));
223 return;
224 }
225 }
226
227 if (appLifeCycleDeal_) {
228 SendEvent(
229 AMSEventHandler::TERMINATE_ABILITY_TIMEOUT_MSG, AMSEventHandler::TERMINATE_ABILITY_TIMEOUT, abilityRecord);
230 appLifeCycleDeal_->ScheduleCleanAbility(token);
231 } else {
232 HILOG_WARN("appLifeCycleDeal_ is null");
233 auto serviceInner = appMgrServiceInner_.lock();
234 if (serviceInner) {
235 serviceInner->TerminateApplication(appRecord);
236 }
237 }
238
239 HILOG_INFO("ModuleRunningRecord::TerminateAbility end");
240 }
241
SendEvent(uint32_t msg,int64_t timeOut,const std::shared_ptr<AbilityRunningRecord> & abilityRecord)242 void ModuleRunningRecord::SendEvent(
243 uint32_t msg, int64_t timeOut, const std::shared_ptr<AbilityRunningRecord> &abilityRecord)
244 {
245 HILOG_INFO("Send event");
246 if (!eventHandler_) {
247 HILOG_ERROR("eventHandler_ is nullptr");
248 return;
249 }
250
251 AppRunningRecord::appEventId_++;
252 abilityRecord->SetEventId(AppRunningRecord::appEventId_);
253 eventHandler_->SendEvent(msg, AppRunningRecord::appEventId_, timeOut);
254 }
255
AbilityTerminated(const sptr<IRemoteObject> & token)256 void ModuleRunningRecord::AbilityTerminated(const sptr<IRemoteObject> &token)
257 {
258 HILOG_INFO("Ability terminated.");
259 if (!token) {
260 HILOG_ERROR("token is null");
261 return;
262 }
263
264 if (RemoveTerminateAbilityTimeoutTask(token)) {
265 terminateAbilities_.erase(token);
266 }
267 }
268
RemoveTerminateAbilityTimeoutTask(const sptr<IRemoteObject> & token) const269 bool ModuleRunningRecord::RemoveTerminateAbilityTimeoutTask(const sptr<IRemoteObject>& token) const
270 {
271 auto abilityRecord = GetAbilityByTerminateLists(token);
272 if (!abilityRecord) {
273 HILOG_ERROR("ModuleRunningRecord::AbilityTerminated can not find ability record");
274 return false;
275 }
276 if (!eventHandler_) {
277 HILOG_ERROR("eventHandler_ is nullptr");
278 return false;
279 }
280 eventHandler_->RemoveEvent(AMSEventHandler::TERMINATE_ABILITY_TIMEOUT_MSG, abilityRecord->GetEventId());
281 return true;
282 }
283
SetAppMgrServiceInner(const std::weak_ptr<AppMgrServiceInner> & inner)284 void ModuleRunningRecord::SetAppMgrServiceInner(const std::weak_ptr<AppMgrServiceInner> &inner)
285 {
286 appMgrServiceInner_ = inner;
287 }
288
GetModuleRecordState()289 ModuleRecordState ModuleRunningRecord::GetModuleRecordState()
290 {
291 return owenState_;
292 }
293
SetModuleRecordState(const ModuleRecordState & state)294 void ModuleRunningRecord::SetModuleRecordState(const ModuleRecordState &state)
295 {
296 owenState_ = state;
297 }
298
GetHapModuleInfo(HapModuleInfo & info)299 void ModuleRunningRecord::GetHapModuleInfo(HapModuleInfo &info)
300 {
301 info = owenInfo_;
302 }
303
SetApplicationClient(std::shared_ptr<AppLifeCycleDeal> & appLifeCycleDeal)304 void ModuleRunningRecord::SetApplicationClient(std::shared_ptr<AppLifeCycleDeal> &appLifeCycleDeal)
305 {
306 appLifeCycleDeal_ = appLifeCycleDeal;
307 }
308
GetState() const309 ModuleRecordState ModuleRunningRecord::GetState() const
310 {
311 return owenState_;
312 }
313 } // namespace AppExecFwk
314 } // namespace OHOS
315