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 #include "hilog_tag_wrapper.h"
16 #include "iability_stage_monitor.h"
17
18 namespace OHOS {
19 namespace AppExecFwk {
IAbilityStageMonitor(const std::string & moduleName,const std::string & srcEntrance)20 IAbilityStageMonitor::IAbilityStageMonitor(const std::string &moduleName, const std::string &srcEntrance)
21 : moduleName_(moduleName), srcEntrance_(srcEntrance)
22 {}
23
Match(const std::shared_ptr<DelegatorAbilityStageProperty> & abilityStage,bool isNotify)24 bool IAbilityStageMonitor::Match(const std::shared_ptr<DelegatorAbilityStageProperty> &abilityStage, bool isNotify)
25 {
26 if (!abilityStage) {
27 TAG_LOGE(AAFwkTag::DELEGATOR, "null abilityStage");
28 return false;
29 }
30 if (moduleName_.compare(abilityStage->moduleName_) != 0 || srcEntrance_.compare(abilityStage->srcEntrance_) != 0) {
31 TAG_LOGW(AAFwkTag::DELEGATOR, "different abilityStage");
32 return false;
33 }
34
35 TAG_LOGI(AAFwkTag::DELEGATOR,
36 "Matched : abilityStage module name : %{public}s, srcEntrance : %{public}s, isNotify : %{public}s",
37 moduleName_.c_str(), srcEntrance_.c_str(), (isNotify ? "true" : "false"));
38
39 if (isNotify) {
40 {
41 std::lock_guard<std::mutex> matchLock(mtxMatch_);
42 matchedAbilityStage_ = abilityStage;
43 }
44 cvMatch_.notify_one();
45 }
46 return true;
47 }
48
WaitForAbilityStage()49 std::shared_ptr<DelegatorAbilityStageProperty> IAbilityStageMonitor::WaitForAbilityStage()
50 {
51 return WaitForAbilityStage(MAX_TIME_OUT);
52 }
53
WaitForAbilityStage(const int64_t timeoutMs)54 std::shared_ptr<DelegatorAbilityStageProperty> IAbilityStageMonitor::WaitForAbilityStage(const int64_t timeoutMs)
55 {
56 auto realTime = timeoutMs;
57 if (timeoutMs <= 0) {
58 TAG_LOGW(AAFwkTag::DELEGATOR, "timeout not positive number");
59 realTime = MAX_TIME_OUT;
60 }
61
62 std::unique_lock<std::mutex> matchLock(mtxMatch_);
63
64 auto condition = [this] { return this->matchedAbilityStage_ != nullptr; };
65 if (!cvMatch_.wait_for(matchLock, std::chrono::milliseconds(realTime), condition)) {
66 TAG_LOGW(AAFwkTag::DELEGATOR, "wait abilityStage timeout");
67 }
68 return matchedAbilityStage_;
69 }
70 } // namespace AppExecFwk
71 } // namespace OHOS
72