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_ACE_COMPONENT_TEST_IDLE_WATCHER_H
17 #define OHOS_ACE_COMPONENT_TEST_IDLE_WATCHER_H
18 
19 #include <functional>
20 #include <mutex>
21 #include <vector>
22 
23 #include "component_test/pipeline_status.h"
24 
25 #include "base/memory/ace_type.h"
26 #include "base/thread/task_executor.h"
27 #include "core/common/task_runner_adapter.h"
28 
29 namespace OHOS::Ace::ComponentTest {
30 using IdleNotifycallback = std::function<void()>;
31 
32 #define CONTINUOUS_IDLE_TIME 5
33 
34 class IdleMonitorThread : public AceType {
35 public:
36     static RefPtr<IdleMonitorThread> Create();
37     void Stop();
38     void SetIdleNotifycallback(IdleNotifycallback);
39     void SetTaskExecutor(RefPtr<TaskExecutor>);
40     void PostInitializeTask();
41 
42 private:
43     RefPtr<TaskRunnerAdapter> thread;
44     IdleNotifycallback callback_;
45     RefPtr<TaskExecutor> taskExecutor_;
46     void VsyncCountFirstCheck();
47     void VsyncCountSecondCheck();
48     void PostCheckTask(std::function<void()>&&, uint32_t);
49 };
50 
51 class IdleWatcher : public AceType {
52 public:
53     IdleWatcher(RefPtr<TaskExecutor> taskExecutor);
54     void RequestNextIdleStatusNotification(IdleNotifycallback&& notifycallback, bool haveUIChange = false);
55     void RequestContinuousIdleStatusNotification(IdleNotifycallback&& continuousIdleCallback);
56     void ClaimLongOperation();
57     void LongOperationComplete();
58 
59 private:
60     std::vector<IdleNotifycallback> pendingIdleObservers_;
61     IdleNotifycallback continuousIdleCallback_;
62     std::mutex observersMutex_;
63     RefPtr<IdleMonitorThread> idleMonitorThread_;
64     bool keepingIdle_;
65     void TriggerIdleNotification();
66     void Destroy();
67     friend class ComponentTestManagerImpl;
68 };
69 } // namespace OHOS::Ace::ComponentTest
70 
71 #endif // OHOS_ACE_COMPONENT_TEST_IDLE_WATCHER_H
72