• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MONITORE_H
17 #define OHOS_ABILITY_RUNTIME_IABILITY_MONITORE_H
18 
19 #include <condition_variable>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 
24 #include "ability_delegator_infos.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 class IAbilityMonitor {
29 public:
30     /**
31      * Indicates that the default timeout is 5 seconds.
32      */
33     static constexpr int64_t MAX_TIME_OUT {5000};
34 
35 public:
36     /**
37      * A constructor used to create a IAbilityMonitor instance with the input parameter passed.
38      *
39      * @param abilityName Indicates the specified ability name for monitoring the lifecycle state changes
40      * of the ability.
41      */
42     explicit IAbilityMonitor(const std::string &abilityName);
43 
44     /**
45      * Default deconstructor used to deconstruct.
46      */
47     virtual ~IAbilityMonitor() = default;
48 
49     /**
50      * Match the monitored Ability objects when newAbility objects are started or
51      * the lifecycle states of monitored abilities have changed.
52      *
53      * @param ability Indicates the ability.
54      * @param isNotify Indicates whether to notify the matched ability to the object who waited.
55      * @return true if match is successful; returns false otherwise.
56      */
57     virtual bool Match(const std::shared_ptr<ADelegatorAbilityProperty> &ability, bool isNotify = false);
58 
59     /**
60      * Waits for and returns the started Ability object that matches the conditions specified in this monitor
61      * within 5 seconds.
62      * The current thread will be blocked until the 5-second default timer expires.
63      *
64      * @return the Ability object if any object has started is matched within 5 seconds; returns null otherwise.
65      */
66     virtual std::shared_ptr<ADelegatorAbilityProperty> WaitForAbility();
67 
68     /**
69      * Waits for and returns the started Ability object that matches the conditions specified in this monitor
70      * within the specified time.
71      * The current thread will be blocked until the timer specified by timeoutMillisecond expires.
72      *
73      * @param timeoutMs Indicates the maximum amount of time to wait, in milliseconds.
74      * The value must be a positive integer.
75      * @return the Ability object if any object has started is matched within the specified time;
76      * returns null otherwise.
77      */
78     virtual std::shared_ptr<ADelegatorAbilityProperty> WaitForAbility(const int64_t timeoutMs);
79 
80     /**
81      * Called when ability is started.
82      *
83      * @param abilityObj Indicates the ability object.
84      */
85     virtual void OnAbilityStart(const std::weak_ptr<NativeReference> &abilityObj);
86 
87     /**
88      * Called when ability is in foreground.
89      *
90      * @param abilityObj Indicates the ability object.
91      */
92     virtual void OnAbilityForeground(const std::weak_ptr<NativeReference> &abilityObj);
93 
94     /**
95      * Called when ability is in background.
96      *
97      * @param abilityObj Indicates the ability object.
98      */
99     virtual void OnAbilityBackground(const std::weak_ptr<NativeReference> &abilityObj);
100 
101     /**
102      * Called when ability is stopped.
103      *
104      * @param abilityObj Indicates the ability object.
105      */
106     virtual void OnAbilityStop(const std::weak_ptr<NativeReference> &abilityObj);
107 
108     /**
109      * Called when window stage is created.
110      *
111      * @param abilityObj Indicates the ability object.
112      */
113     virtual void OnWindowStageCreate(const std::weak_ptr<NativeReference> &abilityObj);
114 
115     /**
116      * Called when window stage is restored.
117      *
118      * @param abilityObj Indicates the ability object.
119      */
120     virtual void OnWindowStageRestore(const std::weak_ptr<NativeReference> &abilityObj);
121 
122     /**
123      * Called when window stage is destroyed.
124      *
125      * @param abilityObj Indicates the ability object.
126      */
127     virtual void OnWindowStageDestroy(const std::weak_ptr<NativeReference> &abilityObj);
128 
129 private:
130     std::string abilityName_;
131     std::shared_ptr<ADelegatorAbilityProperty> matchedAbility_;
132 
133     std::condition_variable cvMatch_;
134     std::mutex mMatch_;
135 };
136 }  // namespace AppExecFwk
137 }  // namespace OHOS
138 
139 #endif  // OHOS_ABILITY_RUNTIME_IABILITY_MONITORE_H
140