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 #ifndef OHOS_ABILITY_RUNTIME_IABILITY_STAGE_MONITORE_H
17 #define OHOS_ABILITY_RUNTIME_IABILITY_STAGE_MONITORE_H
18 
19 #include <condition_variable>
20 #include <memory>
21 #include <string>
22 
23 #include "ability_delegator_infos.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 class IAbilityStageMonitor {
28 public:
29     /**
30      * Indicates that the default timeout is 5 seconds.
31      */
32     static constexpr int64_t MAX_TIME_OUT {5000};
33 
34 public:
35     /**
36      * A constructor used to create a IAbilityStageMonitor instance with the input parameter passed.
37      *
38      * @param moduleName Indicates the specified module name for monitoring the lifecycle state changes
39      * of the abilityStage.
40      * @param srcEntrance Indicates the path of the abilityStage.
41      */
42     explicit IAbilityStageMonitor(const std::string &moduleName, const std::string &srcEntrance);
43 
44     /**
45      * Default deconstructor used to deconstruct.
46      */
47     virtual ~IAbilityStageMonitor() = default;
48 
49     /**
50      * Match the monitored AbilityStage objects when newAbility objects are started.
51      *
52      * @param abilityStage Indicates the abilityStage.
53      * @param isNotify Indicates whether to notify the matched abilityStage to the object who waited.
54      * @return true if match is successful; returns false otherwise.
55      */
56     virtual bool Match(const std::shared_ptr<DelegatorAbilityStageProperty> &abilityStage, bool isNotify = false);
57 
58     /**
59      * Waits for and returns the started AbilityStage object that matches the conditions specified in this monitor
60      * within the specified time.
61      * The current thread will be blocked until the timer specified by timeoutMillisecond expires.
62      *
63      * @param timeoutMs Indicates the maximum amount of time to wait, in milliseconds.
64      * The value must be a positive integer.
65      * @return the AbilityStage object if any object has started is matched within the specified time;
66      * returns null otherwise.
67      */
68     virtual std::shared_ptr<DelegatorAbilityStageProperty> WaitForAbilityStage(const int64_t timeoutMs);
69 
70     /**
71      * Waits for and returns the started AbilityStage object that matches the conditions specified in this monitor
72      * within 5 seconds.
73      * The current thread will be blocked until the 5-second default timer expires.
74      *
75      * @return the AbilityStage object if any object has started is matched within 5 seconds; returns null otherwise.
76      */
77     virtual std::shared_ptr<DelegatorAbilityStageProperty> WaitForAbilityStage();
78 
79 private:
80     std::string moduleName_;
81     std::string srcEntrance_;
82     std::shared_ptr<DelegatorAbilityStageProperty> matchedAbilityStage_;
83 
84     std::condition_variable cvMatch_;
85     std::mutex mtxMatch_;
86 };
87 }  // namespace AppExecFwk
88 }  // namespace OHOS
89 #endif  // OHOS_ABILITY_RUNTIME_IABILITY_STAGE_MONITORE_H
90