• 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_WATCHDOG_H
17 #define OHOS_ABILITY_RUNTIME_WATCHDOG_H
18 
19 #include <string>
20 #include <mutex>
21 #include <thread>
22 #include "event_handler.h"
23 #include "inner_event.h"
24 #include "application_impl.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 constexpr uint32_t INI_TIMER_FIRST_SECOND = 10000;
29 constexpr const char* MAIN_THREAD_TIMEOUT_TASK = "MAIN_THREAD_TIMEOUT_TASK";
30 class Watchdog {
31 public:
32     Watchdog();
33     ~Watchdog();
34 
35     /**
36      *
37      * @brief Init the Watchdog.
38      *
39      * @param mainHandler The handler of main thread.
40      */
41     void Init(const std::shared_ptr<EventHandler> mainHandler);
42 
43     /**
44      *
45      * @brief Stop the mainthread function of watchdog.
46      *
47      */
48     void Stop();
49 
50     /**
51      *
52      * @brief Set the info of application.
53      *
54      * @param applicationInfo The info of application
55      */
56     void SetApplicationInfo(const std::shared_ptr<ApplicationInfo> &applicationInfo);
57 
58     /**
59      *
60      * @brief Set the state of main thread.
61      *
62      * @param appMainThreadState The state of main thread.
63      */
64     void SetAppMainThreadState(const bool appMainThreadState);
65 
66     /**
67      *
68      * @brief Set whether app is in the background or not
69      *
70      */
71     void SetBackgroundStatus(const bool isInBackground);
72 
73     /**
74      *
75      * @brief Allow report the main thread timeout event.
76      *
77      */
78     void AllowReportEvent();
79 
80     /**
81      *
82      * @brief Get StopWatchdog flag.
83      *
84      */
85     bool IsStopWatchdog();
86 
87     /**
88      *
89      * @brief Check and reset the main thread state.
90      *
91      */
92     bool IsReportEvent();
93 
94 private:
95     void Timer();
96     void reportEvent();
97 
98     std::atomic_bool appMainThreadIsAlive_ = false;
99     std::atomic_bool stopWatchdog_ = false;
100     std::atomic_bool needReport_ = true;
101     std::atomic_bool isSixSecondEvent_ = false;
102     std::atomic_bool isInBackground_ = false;
103     std::shared_ptr<ApplicationInfo> applicationInfo_ = nullptr;
104     std::mutex cvMutex_;
105     std::condition_variable cvWatchdog_;
106     static std::shared_ptr<EventHandler> appMainHandler_;
107     int64_t lastWatchTime_ = 0;
108 };
109 
110 class MainHandlerDumper : public Dumper {
111 public:
112     virtual void Dump(const std::string &message) override;
113     virtual std::string GetTag() override;
114     std::string GetDumpInfo();
115 private:
116     std::string dumpInfo;
117 };
118 }  // namespace AppExecFwk
119 }  // namespace OHOS
120 #endif  // OHOS_ABILITY_RUNTIME_WATCHDOG_H
121