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