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 <chrono>
16 #include "hilog_tag_wrapper.h"
17 #include "iability_monitor.h"
18 
19 using namespace std::chrono_literals;
20 
21 namespace OHOS {
22 namespace AppExecFwk {
IAbilityMonitor(const std::string & abilityName)23 IAbilityMonitor::IAbilityMonitor(const std::string &abilityName) : abilityName_(abilityName)
24 {}
25 
IAbilityMonitor(const std::string & abilityName,const std::string & moduleName)26 IAbilityMonitor::IAbilityMonitor(const std::string &abilityName,
27     const std::string &moduleName) : abilityName_(abilityName), moduleName_(moduleName)
28 {}
29 
Match(const std::shared_ptr<ADelegatorAbilityProperty> & ability,bool isNotify)30 bool IAbilityMonitor::Match(const std::shared_ptr<ADelegatorAbilityProperty> &ability, bool isNotify)
31 {
32     if (!ability) {
33         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid ability property");
34         return false;
35     }
36 
37     const auto &aName = ability->name_;
38 
39     if (abilityName_.empty() || aName.empty()) {
40         TAG_LOGW(AAFwkTag::DELEGATOR, "invalid name");
41         return false;
42     }
43 
44     if (abilityName_.compare(aName)) {
45         TAG_LOGW(AAFwkTag::DELEGATOR, "different name");
46         return false;
47     }
48 
49     const auto &aModuleName = ability->moduleName_;
50 
51     if (!moduleName_.empty() && moduleName_.compare(aModuleName) != 0) {
52         TAG_LOGE(AAFwkTag::DELEGATOR, "different moduleName, %{public}s and %{public}s",
53             moduleName_.c_str(), aModuleName.c_str());
54         return false;
55     }
56 
57     TAG_LOGI(AAFwkTag::DELEGATOR, "ability name : %{public}s, isNotify : %{public}s",
58         abilityName_.data(), (isNotify ? "true" : "false"));
59 
60     if (isNotify) {
61         TAG_LOGI(AAFwkTag::DELEGATOR, "notify ability matched");
62         {
63             std::lock_guard<std::mutex> matchLock(mMatch_);
64             matchedAbility_ = ability;
65         }
66         cvMatch_.notify_one();
67     }
68 
69     return true;
70 }
71 
WaitForAbility()72 std::shared_ptr<ADelegatorAbilityProperty> IAbilityMonitor::WaitForAbility()
73 {
74     return WaitForAbility(MAX_TIME_OUT);
75 }
76 
WaitForAbility(const int64_t timeoutMs)77 std::shared_ptr<ADelegatorAbilityProperty> IAbilityMonitor::WaitForAbility(const int64_t timeoutMs)
78 {
79     auto realTime = timeoutMs;
80     if (timeoutMs <= 0) {
81         TAG_LOGW(AAFwkTag::DELEGATOR, "timeout should not number");
82         realTime = MAX_TIME_OUT;
83     }
84 
85     std::unique_lock<std::mutex> matchLock(mMatch_);
86 
87     auto condition = [this] { return this->matchedAbility_ != nullptr; };
88     if (!cvMatch_.wait_for(matchLock, realTime * 1ms, condition)) {
89         TAG_LOGW(AAFwkTag::DELEGATOR, "wait ability timeout");
90     }
91 
92     return matchedAbility_;
93 }
94 
OnAbilityStart(const std::weak_ptr<NativeReference> & abilityObj)95 void IAbilityMonitor::OnAbilityStart(const std::weak_ptr<NativeReference> &abilityObj)
96 {}
97 
OnAbilityForeground(const std::weak_ptr<NativeReference> & abilityObj)98 void IAbilityMonitor::OnAbilityForeground(const std::weak_ptr<NativeReference> &abilityObj)
99 {}
100 
OnAbilityBackground(const std::weak_ptr<NativeReference> & abilityObj)101 void IAbilityMonitor::OnAbilityBackground(const std::weak_ptr<NativeReference> &abilityObj)
102 {}
103 
OnAbilityStop(const std::weak_ptr<NativeReference> & abilityObj)104 void IAbilityMonitor::OnAbilityStop(const std::weak_ptr<NativeReference> &abilityObj)
105 {}
106 
OnWindowStageCreate(const std::weak_ptr<NativeReference> & abilityObj)107 void IAbilityMonitor::OnWindowStageCreate(const std::weak_ptr<NativeReference> &abilityObj)
108 {}
109 
OnWindowStageRestore(const std::weak_ptr<NativeReference> & abilityObj)110 void IAbilityMonitor::OnWindowStageRestore(const std::weak_ptr<NativeReference> &abilityObj)
111 {}
112 
OnWindowStageDestroy(const std::weak_ptr<NativeReference> & abilityObj)113 void IAbilityMonitor::OnWindowStageDestroy(const std::weak_ptr<NativeReference> &abilityObj)
114 {}
115 }  // namespace AppExecFwk
116 }  // namespace OHOS
117