1 /*
2  * Copyright (c) 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 
16 #ifndef OHOS_ABILITY_RUNTIME_ASSERT_FAULT_TASK_THREAD_H
17 #define OHOS_ABILITY_RUNTIME_ASSERT_FAULT_TASK_THREAD_H
18 
19 #include <chrono>
20 #include <memory>
21 #include <mutex>
22 
23 #include "ability_state.h"
24 #include "event_handler.h"
25 #include "event_runner.h"
26 #include "iremote_object.h"
27 #include "singleton.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 class MainThread;
32 } // namespace AppExecFwk
33 namespace AbilityRuntime {
34 class AssertFaultTaskThread : public DelayedSingleton<AssertFaultTaskThread>,
35     public std::enable_shared_from_this<AssertFaultTaskThread> {
36     DISALLOW_COPY_AND_MOVE(AssertFaultTaskThread);
37 public:
38     AssertFaultTaskThread() = default;
39     virtual ~AssertFaultTaskThread() = default;
40 
41     static std::shared_ptr<AssertFaultTaskThread> GetInstance();
42 
43     void InitAssertFaultTask(const wptr<AppExecFwk::MainThread> &weak, bool isDebugModule);
44     void NotifyReleaseLongWaiting();
45     void Stop();
46     AAFwk::UserStatus RequestAssertResult(const std::string &exprStr);
47 
48 private:
49     AAFwk::UserStatus HandleAssertCallback(const std::string &exprStr);
50 
51 private:
52     wptr<AppExecFwk::MainThread> mainThread_;
53     std::mutex assertResultMutex_;
54     std::condition_variable assertResultCV_;
55     bool isDebugModule_ = false;
56     std::shared_ptr<AppExecFwk::EventRunner> assertRunner_ = nullptr;
57     std::shared_ptr<AppExecFwk::EventHandler> assertHandler_ = nullptr;
58 };
59 } // namespace AbilityRuntime
60 } // namespace OHOS
61 #endif  // OHOS_ABILITY_RUNTIME_ASSERT_FAULT_TASK_THREAD_H