1 /*
2  * Copyright (c) 2022-2024 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 "ability_delegator.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "ohos_application.h"
20 #include "ability_manager_client.h"
21 #include "ability_delegator_registry.h"
22 #include "itest_observer.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
AbilityDelegator(const std::shared_ptr<AbilityRuntime::Context> & context,std::unique_ptr<TestRunner> runner,const sptr<IRemoteObject> & observer)26 AbilityDelegator::AbilityDelegator(const std::shared_ptr<AbilityRuntime::Context> &context,
27     std::unique_ptr<TestRunner> runner, const sptr<IRemoteObject> &observer)
28     : appContext_(context), testRunner_(std::move(runner)), observer_(observer)
29 {}
30 
~AbilityDelegator()31 AbilityDelegator::~AbilityDelegator()
32 {}
33 
AddAbilityMonitor(const std::shared_ptr<IAbilityMonitor> & monitor)34 void AbilityDelegator::AddAbilityMonitor(const std::shared_ptr<IAbilityMonitor> &monitor)
35 {
36     if (!monitor) {
37         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
38         return;
39     }
40 
41     std::unique_lock<std::mutex> lck(mutexMonitor_);
42     auto pos = std::find(abilityMonitors_.begin(), abilityMonitors_.end(), monitor);
43     if (pos != abilityMonitors_.end()) {
44         TAG_LOGW(AAFwkTag::DELEGATOR, "monitor has been added");
45         return;
46     }
47 
48     abilityMonitors_.emplace_back(monitor);
49 }
50 
AddAbilityStageMonitor(const std::shared_ptr<IAbilityStageMonitor> & monitor)51 void AbilityDelegator::AddAbilityStageMonitor(const std::shared_ptr<IAbilityStageMonitor> &monitor)
52 {
53     if (!monitor) {
54         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
55         return;
56     }
57     std::unique_lock<std::mutex> lck(mutexStageMonitor_);
58     auto pos = std::find(abilityStageMonitors_.begin(), abilityStageMonitors_.end(), monitor);
59     if (pos != abilityStageMonitors_.end()) {
60         TAG_LOGW(AAFwkTag::DELEGATOR, "stage monitor added");
61         return;
62     }
63     abilityStageMonitors_.emplace_back(monitor);
64 }
65 
RemoveAbilityMonitor(const std::shared_ptr<IAbilityMonitor> & monitor)66 void AbilityDelegator::RemoveAbilityMonitor(const std::shared_ptr<IAbilityMonitor> &monitor)
67 {
68     if (!monitor) {
69         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
70         return;
71     }
72 
73     std::unique_lock<std::mutex> lck(mutexMonitor_);
74     auto pos = std::find(abilityMonitors_.begin(), abilityMonitors_.end(), monitor);
75     if (pos != abilityMonitors_.end()) {
76         abilityMonitors_.erase(pos);
77     }
78 }
79 
RemoveAbilityStageMonitor(const std::shared_ptr<IAbilityStageMonitor> & monitor)80 void AbilityDelegator::RemoveAbilityStageMonitor(const std::shared_ptr<IAbilityStageMonitor> &monitor)
81 {
82     if (!monitor) {
83         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
84         return;
85     }
86     std::unique_lock<std::mutex> lck(mutexStageMonitor_);
87     auto pos = std::find(abilityStageMonitors_.begin(), abilityStageMonitors_.end(), monitor);
88     if (pos != abilityStageMonitors_.end()) {
89         abilityStageMonitors_.erase(pos);
90     }
91 }
92 
ClearAllMonitors()93 void AbilityDelegator::ClearAllMonitors()
94 {
95     std::unique_lock<std::mutex> lck(mutexMonitor_);
96     abilityMonitors_.clear();
97 }
98 
GetMonitorsNum()99 size_t AbilityDelegator::GetMonitorsNum()
100 {
101     std::unique_lock<std::mutex> lck(mutexMonitor_);
102     return abilityMonitors_.size();
103 }
104 
GetStageMonitorsNum()105 size_t AbilityDelegator::GetStageMonitorsNum()
106 {
107     std::unique_lock<std::mutex> lck(mutexStageMonitor_);
108     return abilityStageMonitors_.size();
109 }
110 
111 
WaitAbilityMonitor(const std::shared_ptr<IAbilityMonitor> & monitor)112 std::shared_ptr<ADelegatorAbilityProperty> AbilityDelegator::WaitAbilityMonitor(
113     const std::shared_ptr<IAbilityMonitor> &monitor)
114 {
115     if (!monitor) {
116         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
117         return {};
118     }
119 
120     AddAbilityMonitor(monitor);
121 
122     auto obtainedAbility = monitor->WaitForAbility();
123     if (!obtainedAbility) {
124         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid obtained ability");
125         return {};
126     }
127 
128     return obtainedAbility;
129 }
130 
WaitAbilityStageMonitor(const std::shared_ptr<IAbilityStageMonitor> & monitor)131 std::shared_ptr<DelegatorAbilityStageProperty> AbilityDelegator::WaitAbilityStageMonitor(
132     const std::shared_ptr<IAbilityStageMonitor> &monitor)
133 {
134     if (!monitor) {
135         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid monitor");
136         return nullptr;
137     }
138 
139     AddAbilityStageMonitor(monitor);
140     auto stage = monitor->WaitForAbilityStage();
141     if (!stage) {
142         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid abilityStage");
143         return nullptr;
144     }
145     return stage;
146 }
147 
WaitAbilityMonitor(const std::shared_ptr<IAbilityMonitor> & monitor,const int64_t timeoutMs)148 std::shared_ptr<ADelegatorAbilityProperty> AbilityDelegator::WaitAbilityMonitor(
149     const std::shared_ptr<IAbilityMonitor> &monitor, const int64_t timeoutMs)
150 {
151     if (!monitor) {
152         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
153         return {};
154     }
155 
156     AddAbilityMonitor(monitor);
157 
158     auto obtainedAbility = monitor->WaitForAbility(timeoutMs);
159     if (!obtainedAbility) {
160         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid obtained ability");
161         return {};
162     }
163 
164     return obtainedAbility;
165 }
166 
WaitAbilityStageMonitor(const std::shared_ptr<IAbilityStageMonitor> & monitor,const int64_t timeoutMs)167 std::shared_ptr<DelegatorAbilityStageProperty> AbilityDelegator::WaitAbilityStageMonitor(
168     const std::shared_ptr<IAbilityStageMonitor> &monitor, const int64_t timeoutMs)
169 {
170     if (!monitor) {
171         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid monitor");
172         return nullptr;
173     }
174     AddAbilityStageMonitor(monitor);
175     auto stage = monitor->WaitForAbilityStage(timeoutMs);
176     if (!stage) {
177         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid obtained abilityStage");
178         return nullptr;
179     }
180     return stage;
181 }
182 
GetAppContext() const183 std::shared_ptr<AbilityRuntime::Context> AbilityDelegator::GetAppContext() const
184 {
185     return appContext_;
186 }
187 
GetAbilityState(const sptr<IRemoteObject> & token)188 AbilityDelegator::AbilityState AbilityDelegator::GetAbilityState(const sptr<IRemoteObject> &token)
189 {
190     if (!token) {
191         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
192         return AbilityDelegator::AbilityState::UNINITIALIZED;
193     }
194 
195     std::unique_lock<std::mutex> lck(mutexAbilityProperties_);
196     auto existedProperty = FindPropertyByToken(token);
197     if (!existedProperty) {
198         TAG_LOGW(AAFwkTag::DELEGATOR, "unknown ability token");
199         return AbilityDelegator::AbilityState::UNINITIALIZED;
200     }
201 
202     return ConvertAbilityState(existedProperty->lifecycleState_);
203 }
204 
GetCurrentTopAbility()205 std::shared_ptr<ADelegatorAbilityProperty> AbilityDelegator::GetCurrentTopAbility()
206 {
207     AppExecFwk::ElementName elementName = AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility();
208     std::string bundleName = elementName.GetBundleName();
209     std::string abilityName = elementName.GetAbilityName();
210     if (abilityName.empty()) {
211         TAG_LOGE(AAFwkTag::DELEGATOR, "get topAbility failed");
212         return {};
213     }
214 
215     if (!bundleName.empty()) {
216         std::string::size_type pos = abilityName.find(bundleName);
217         if (pos == std::string::npos || pos != 0) {
218             abilityName = bundleName + "." + abilityName;
219         }
220     }
221 
222     std::unique_lock<std::mutex> lck(mutexAbilityProperties_);
223     auto existedProperty = FindPropertyByName(abilityName);
224     if (!existedProperty) {
225         TAG_LOGW(AAFwkTag::DELEGATOR, "unknown ability name");
226         return {};
227     }
228 
229     return existedProperty;
230 }
231 
GetThreadName() const232 std::string AbilityDelegator::GetThreadName() const
233 {
234     return {};
235 }
236 
Prepare()237 void AbilityDelegator::Prepare()
238 {
239     TAG_LOGI(AAFwkTag::DELEGATOR, "Enter");
240     if (!testRunner_) {
241         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid TestRunner");
242         return;
243     }
244 
245     TAG_LOGD(AAFwkTag::DELEGATOR, "call js onPrepare()");
246     testRunner_->Prepare();
247 
248     if (!delegatorThread_) {
249         delegatorThread_ = std::make_unique<DelegatorThread>(true);
250         if (!delegatorThread_) {
251             TAG_LOGE(AAFwkTag::DELEGATOR, "create delegatorThread failed");
252             return;
253         }
254     }
255 
256     auto runTask = [this]() { this->OnRun(); };
257     if (!delegatorThread_->Run(runTask)) {
258         TAG_LOGE(AAFwkTag::DELEGATOR, "run task on delegatorThread failed");
259     }
260 }
261 
OnRun()262 void AbilityDelegator::OnRun()
263 {
264     TAG_LOGI(AAFwkTag::DELEGATOR, "Enter");
265     if (!testRunner_) {
266         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid TestRunner");
267         return;
268     }
269 
270     TAG_LOGD(AAFwkTag::DELEGATOR, "call js onRun()");
271     testRunner_->Run();
272 }
273 
StartAbility(const AAFwk::Want & want)274 ErrCode AbilityDelegator::StartAbility(const AAFwk::Want &want)
275 {
276     TAG_LOGI(AAFwkTag::DELEGATOR, "Enter");
277 
278     auto realWant(want);
279     auto delegatorArgs = AbilityDelegatorRegistry::GetArguments();
280     if (delegatorArgs && delegatorArgs->FindDebugFlag()) {
281         TAG_LOGI(AAFwkTag::DELEGATOR, "start with debug");
282         realWant.SetParam("debugApp", true);
283     }
284     realWant.SetParam(IS_DELEGATOR_CALL, true);
285 
286     return AbilityManagerClient::GetInstance()->StartAbility(realWant);
287 }
288 
DoAbilityForeground(const sptr<IRemoteObject> & token)289 bool AbilityDelegator::DoAbilityForeground(const sptr<IRemoteObject> &token)
290 {
291     TAG_LOGI(AAFwkTag::DELEGATOR, "called");
292 
293     if (!token) {
294         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
295         return false;
296     }
297 
298     auto ret = AAFwk::AbilityManagerClient::GetInstance()->DelegatorDoAbilityForeground(token);
299     if (ret) {
300         TAG_LOGE(AAFwkTag::DELEGATOR, "call DelegatorDoAbilityForeground failed, reson: %{public}d", ret);
301         return false;
302     }
303 
304     return true;
305 }
306 
DoAbilityBackground(const sptr<IRemoteObject> & token)307 bool AbilityDelegator::DoAbilityBackground(const sptr<IRemoteObject> &token)
308 {
309     TAG_LOGI(AAFwkTag::DELEGATOR, "Enter");
310 
311     if (!token) {
312         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
313         return false;
314     }
315 
316     auto ret = AAFwk::AbilityManagerClient::GetInstance()->DelegatorDoAbilityBackground(token);
317     if (ret) {
318         TAG_LOGE(AAFwkTag::DELEGATOR, "call doAbilityBackground failed, reson: %{public}d", ret);
319         return false;
320     }
321 
322     return true;
323 }
324 
ExecuteShellCommand(const std::string & cmd,const int64_t timeoutSec)325 std::unique_ptr<ShellCmdResult> AbilityDelegator::ExecuteShellCommand(const std::string &cmd, const int64_t timeoutSec)
326 {
327     TAG_LOGI(AAFwkTag::DELEGATOR, "command : %{public}s, timeout : %{public}" PRId64, cmd.data(), timeoutSec);
328 
329     if (cmd.empty()) {
330         TAG_LOGE(AAFwkTag::DELEGATOR, "invalid cmd");
331         return {};
332     }
333 
334     auto testObserver = iface_cast<ITestObserver>(observer_);
335     if (!testObserver) {
336         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid testObserver");
337         return {};
338     }
339 
340     auto result = testObserver->ExecuteShellCommand(cmd, timeoutSec);
341     return std::make_unique<ShellCmdResult>(result);
342 }
343 
Print(const std::string & msg)344 void AbilityDelegator::Print(const std::string &msg)
345 {
346     TAG_LOGI(AAFwkTag::DELEGATOR, "Enter");
347 
348     auto testObserver = iface_cast<ITestObserver>(observer_);
349     if (!testObserver) {
350         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid testObserver");
351         return;
352     }
353 
354     auto realMsg(msg);
355     if (realMsg.length() > DELEGATOR_PRINT_MAX_LENGTH) {
356         TAG_LOGW(AAFwkTag::DELEGATOR, "too long message");
357         realMsg.resize(DELEGATOR_PRINT_MAX_LENGTH);
358     }
359     TAG_LOGI(AAFwkTag::DELEGATOR, "message to print : %{public}s", realMsg.data());
360 
361     testObserver->TestStatus(realMsg, 0);
362 }
363 
PostPerformStart(const std::shared_ptr<ADelegatorAbilityProperty> & ability)364 void AbilityDelegator::PostPerformStart(const std::shared_ptr<ADelegatorAbilityProperty> &ability)
365 {
366     TAG_LOGI(AAFwkTag::DELEGATOR, "called");
367 
368     if (!ability) {
369         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
370         return;
371     }
372 
373     ProcessAbilityProperties(ability);
374 
375     std::unique_lock<std::mutex> lck(mutexMonitor_);
376     if (abilityMonitors_.empty()) {
377         TAG_LOGW(AAFwkTag::DELEGATOR, "empty abilityMonitors");
378         return;
379     }
380 
381     for (auto &monitor : abilityMonitors_) {
382         if (!monitor) {
383             continue;
384         }
385 
386         if (monitor->Match(ability, true)) {
387             monitor->OnAbilityStart(ability->object_);
388         }
389     }
390 }
391 
PostPerformStageStart(const std::shared_ptr<DelegatorAbilityStageProperty> & abilityStage)392 void AbilityDelegator::PostPerformStageStart(const std::shared_ptr<DelegatorAbilityStageProperty> &abilityStage)
393 {
394     TAG_LOGI(AAFwkTag::DELEGATOR, "called");
395     if (!abilityStage) {
396         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
397         return;
398     }
399 
400     std::unique_lock<std::mutex> lck(mutexStageMonitor_);
401     if (abilityStageMonitors_.empty()) {
402         TAG_LOGW(AAFwkTag::DELEGATOR, "null abilityStageMonitors");
403         return;
404     }
405 
406     for (auto &monitor : abilityStageMonitors_) {
407         if (!monitor) {
408             continue;
409         }
410         monitor->Match(abilityStage, true);
411     }
412 }
413 
PostPerformScenceCreated(const std::shared_ptr<ADelegatorAbilityProperty> & ability)414 void AbilityDelegator::PostPerformScenceCreated(const std::shared_ptr<ADelegatorAbilityProperty> &ability)
415 {
416     TAG_LOGI(AAFwkTag::DELEGATOR, "called");
417 
418     if (!ability) {
419         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
420         return;
421     }
422 
423     ProcessAbilityProperties(ability);
424 
425     std::unique_lock<std::mutex> lck(mutexMonitor_);
426     if (abilityMonitors_.empty()) {
427         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid abilityMonitors");
428         return;
429     }
430 
431     for (auto &monitor : abilityMonitors_) {
432         if (!monitor) {
433             continue;
434         }
435 
436         if (monitor->Match(ability)) {
437             monitor->OnWindowStageCreate(ability->object_);
438         }
439     }
440 }
441 
PostPerformScenceRestored(const std::shared_ptr<ADelegatorAbilityProperty> & ability)442 void AbilityDelegator::PostPerformScenceRestored(const std::shared_ptr<ADelegatorAbilityProperty> &ability)
443 {
444     TAG_LOGI(AAFwkTag::DELEGATOR, "called");
445 
446     if (!ability) {
447         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
448         return;
449     }
450 
451     ProcessAbilityProperties(ability);
452 
453     std::unique_lock<std::mutex> lck(mutexMonitor_);
454     if (abilityMonitors_.empty()) {
455         TAG_LOGW(AAFwkTag::DELEGATOR, "null abilityMonitors");
456         return;
457     }
458 
459     for (auto &monitor : abilityMonitors_) {
460         if (!monitor) {
461             continue;
462         }
463 
464         if (monitor->Match(ability)) {
465             monitor->OnWindowStageRestore(ability->object_);
466         }
467     }
468 }
469 
PostPerformScenceDestroyed(const std::shared_ptr<ADelegatorAbilityProperty> & ability)470 void AbilityDelegator::PostPerformScenceDestroyed(const std::shared_ptr<ADelegatorAbilityProperty> &ability)
471 {
472     TAG_LOGI(AAFwkTag::DELEGATOR, "called");
473 
474     if (!ability) {
475         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
476         return;
477     }
478 
479     ProcessAbilityProperties(ability);
480 
481     std::unique_lock<std::mutex> lck(mutexMonitor_);
482     if (abilityMonitors_.empty()) {
483         TAG_LOGW(AAFwkTag::DELEGATOR, "null abilityMonitors");
484         return;
485     }
486 
487     for (auto &monitor : abilityMonitors_) {
488         if (!monitor) {
489             continue;
490         }
491 
492         if (monitor->Match(ability)) {
493             monitor->OnWindowStageDestroy(ability->object_);
494         }
495     }
496 }
497 
PostPerformForeground(const std::shared_ptr<ADelegatorAbilityProperty> & ability)498 void AbilityDelegator::PostPerformForeground(const std::shared_ptr<ADelegatorAbilityProperty> &ability)
499 {
500     TAG_LOGI(AAFwkTag::DELEGATOR, "called");
501 
502     if (!ability) {
503         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
504         return;
505     }
506 
507     ProcessAbilityProperties(ability);
508 
509     std::unique_lock<std::mutex> lck(mutexMonitor_);
510     if (abilityMonitors_.empty()) {
511         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid abilityMonitors");
512         return;
513     }
514 
515     for (auto &monitor : abilityMonitors_) {
516         if (!monitor) {
517             continue;
518         }
519 
520         if (monitor->Match(ability)) {
521             monitor->OnAbilityForeground(ability->object_);
522         }
523     }
524 }
525 
PostPerformBackground(const std::shared_ptr<ADelegatorAbilityProperty> & ability)526 void AbilityDelegator::PostPerformBackground(const std::shared_ptr<ADelegatorAbilityProperty> &ability)
527 {
528     TAG_LOGI(AAFwkTag::DELEGATOR, "called");
529 
530     if (!ability) {
531         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
532         return;
533     }
534 
535     ProcessAbilityProperties(ability);
536 
537     std::unique_lock<std::mutex> lck(mutexMonitor_);
538     if (abilityMonitors_.empty()) {
539         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid abilityMonitors");
540         return;
541     }
542 
543     for (auto &monitor : abilityMonitors_) {
544         if (!monitor) {
545             continue;
546         }
547 
548         if (monitor->Match(ability)) {
549             monitor->OnAbilityBackground(ability->object_);
550         }
551     }
552 }
553 
PostPerformStop(const std::shared_ptr<ADelegatorAbilityProperty> & ability)554 void AbilityDelegator::PostPerformStop(const std::shared_ptr<ADelegatorAbilityProperty> &ability)
555 {
556     TAG_LOGI(AAFwkTag::DELEGATOR, "Enter");
557 
558     if (!ability) {
559         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
560         return;
561     }
562 
563     ProcessAbilityProperties(ability);
564 
565     std::unique_lock<std::mutex> lck(mutexMonitor_);
566     if (abilityMonitors_.empty()) {
567         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid abilityMonitors");
568         return;
569     }
570 
571     for (auto &monitor : abilityMonitors_) {
572         if (!monitor) {
573             continue;
574         }
575 
576         if (monitor->Match(ability)) {
577             monitor->OnAbilityStop(ability->object_);
578         }
579     }
580 
581     RemoveAbilityProperty(ability);
582     CallClearFunc(ability);
583 }
584 
ConvertAbilityState(const AbilityLifecycleExecutor::LifecycleState lifecycleState)585 AbilityDelegator::AbilityState AbilityDelegator::ConvertAbilityState(
586     const AbilityLifecycleExecutor::LifecycleState lifecycleState)
587 {
588     AbilityDelegator::AbilityState abilityState {AbilityDelegator::AbilityState::UNINITIALIZED};
589     switch (lifecycleState) {
590         case AbilityLifecycleExecutor::LifecycleState::STARTED_NEW:
591             abilityState = AbilityDelegator::AbilityState::STARTED;
592             break;
593         case AbilityLifecycleExecutor::LifecycleState::FOREGROUND_NEW:
594             abilityState = AbilityDelegator::AbilityState::FOREGROUND;
595             break;
596         case AbilityLifecycleExecutor::LifecycleState::BACKGROUND_NEW:
597             abilityState = AbilityDelegator::AbilityState::BACKGROUND;
598             break;
599         case AbilityLifecycleExecutor::LifecycleState::STOPED_NEW:
600             abilityState = AbilityDelegator::AbilityState::STOPPED;
601             break;
602         default:
603             TAG_LOGE(AAFwkTag::DELEGATOR, "Unknown lifecycleState");
604             break;
605     }
606 
607     return abilityState;
608 }
609 
ProcessAbilityProperties(const std::shared_ptr<ADelegatorAbilityProperty> & ability)610 void AbilityDelegator::ProcessAbilityProperties(const std::shared_ptr<ADelegatorAbilityProperty> &ability)
611 {
612     TAG_LOGI(AAFwkTag::DELEGATOR, "Enter");
613 
614     if (!ability) {
615         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid ability property");
616         return;
617     }
618 
619     TAG_LOGW(AAFwkTag::DELEGATOR, "ability property : name : %{public}s, state : %{public}d",
620         ability->name_.data(), ability->lifecycleState_);
621 
622     std::unique_lock<std::mutex> lck(mutexAbilityProperties_);
623     auto existedProperty = FindPropertyByToken(ability->token_);
624     if (existedProperty) {
625         // update
626         existedProperty->lifecycleState_ = ability->lifecycleState_;
627         return;
628     }
629 
630     abilityProperties_.emplace_back(ability);
631 }
632 
RemoveAbilityProperty(const std::shared_ptr<ADelegatorAbilityProperty> & ability)633 void AbilityDelegator::RemoveAbilityProperty(const std::shared_ptr<ADelegatorAbilityProperty> &ability)
634 {
635     TAG_LOGD(AAFwkTag::DELEGATOR, "called");
636 
637     if (!ability) {
638         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid ability property");
639         return;
640     }
641 
642     TAG_LOGI(AAFwkTag::DELEGATOR, "ability property { name : %{public}s, state : %{public}d }",
643         ability->name_.data(), ability->lifecycleState_);
644 
645     std::unique_lock<std::mutex> lck(mutexAbilityProperties_);
646     abilityProperties_.remove_if([ability](const auto &properties) {
647         return ability->fullName_ == properties->fullName_;
648     });
649 }
650 
FindPropertyByToken(const sptr<IRemoteObject> & token)651 std::shared_ptr<ADelegatorAbilityProperty> AbilityDelegator::FindPropertyByToken(const sptr<IRemoteObject> &token)
652 {
653     TAG_LOGI(AAFwkTag::DELEGATOR, "Enter");
654 
655     if (!token) {
656         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
657         return {};
658     }
659 
660     for (const auto &it : abilityProperties_) {
661         if (!it) {
662             TAG_LOGW(AAFwkTag::DELEGATOR, "invalid ability property");
663             continue;
664         }
665 
666         if (token == it->token_) {
667             TAG_LOGD(AAFwkTag::DELEGATOR, "property exist");
668             return it;
669         }
670     }
671 
672     return {};
673 }
674 
FindPropertyByName(const std::string & name)675 std::shared_ptr<ADelegatorAbilityProperty> AbilityDelegator::FindPropertyByName(const std::string &name)
676 {
677     TAG_LOGI(AAFwkTag::DELEGATOR, "find property by %{public}s", name.c_str());
678 
679     if (name.empty()) {
680         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid params");
681         return {};
682     }
683 
684     for (const auto &it : abilityProperties_) {
685         if (!it) {
686             TAG_LOGW(AAFwkTag::DELEGATOR, "invalid ability property");
687             continue;
688         }
689 
690         if (name == it->fullName_) {
691             TAG_LOGI(AAFwkTag::DELEGATOR, "property exist");
692             return it;
693         }
694     }
695 
696     return {};
697 }
698 
FinishUserTest(const std::string & msg,const int64_t resultCode)699 void AbilityDelegator::FinishUserTest(const std::string &msg, const int64_t resultCode)
700 {
701     TAG_LOGI(AAFwkTag::DELEGATOR, "msg: %{public}s, code: %{public}" PRId64, msg.data(), resultCode);
702 
703     if (!observer_) {
704         TAG_LOGE(AAFwkTag::DELEGATOR, "invalid observer");
705         return;
706     }
707 
708     auto delegatorArgs = AbilityDelegatorRegistry::GetArguments();
709     if (!delegatorArgs) {
710         TAG_LOGE(AAFwkTag::DELEGATOR, "invalid args");
711         return;
712     }
713 
714     auto realMsg(msg);
715     if (realMsg.length() > INFORMATION_MAX_LENGTH) {
716         TAG_LOGW(AAFwkTag::DELEGATOR, "too long message");
717         realMsg.resize(INFORMATION_MAX_LENGTH);
718     }
719 
720     const auto &bundleName = delegatorArgs->GetTestBundleName();
721     auto err = AAFwk::AbilityManagerClient::GetInstance()->FinishUserTest(realMsg, resultCode, bundleName);
722     if (err) {
723         TAG_LOGE(AAFwkTag::DELEGATOR, "call FinishUserTest failed: %{public}d", err);
724     }
725 }
726 
RegisterClearFunc(ClearFunc func)727 void AbilityDelegator::RegisterClearFunc(ClearFunc func)
728 {
729     TAG_LOGI(AAFwkTag::DELEGATOR, "Enter");
730     if (!func) {
731         TAG_LOGE(AAFwkTag::DELEGATOR, "invalid func");
732         return;
733     }
734 
735     clearFunc_ = func;
736 }
737 
CallClearFunc(const std::shared_ptr<ADelegatorAbilityProperty> & ability)738 inline void AbilityDelegator::CallClearFunc(const std::shared_ptr<ADelegatorAbilityProperty> &ability)
739 {
740     TAG_LOGI(AAFwkTag::DELEGATOR, "Enter");
741     if (clearFunc_) {
742         clearFunc_(ability);
743     }
744 }
745 }  // namespace AppExecFwk
746 }  // namespace OHOS
747