1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef ResponsivenessTimer_h
27#define ResponsivenessTimer_h
28
29#include <wtf/RunLoop.h>
30
31namespace WebKit {
32
33class ResponsivenessTimer {
34public:
35 class Client {
36 public:
37 virtual ~Client() { }
38 virtual void didBecomeUnresponsive() = 0;
39 virtual void didBecomeResponsive() = 0;
40
41 virtual void willChangeIsResponsive() = 0;
42 virtual void didChangeIsResponsive() = 0;
43
44 virtual bool mayBecomeUnresponsive() = 0;
45 };
46
47 explicit ResponsivenessTimer(ResponsivenessTimer::Client&);
48 ~ResponsivenessTimer();
49
50 void start();
51
52 // A responsiveness timer with lazy stop does not stop the underlying system timer when stopped.
53 // Instead, it ignores the timeout if stop() was already called.
54 //
55 // This exists to reduce the rate at which we reset the timer.
56 //
57 // With a non lazy timer, we may set a timer and reset it soon after because the process is responsive.
58 // For events, this means reseting a timer 120 times/s for a 60 Hz event source.
59 // By not reseting the timer when responsive, we cut that in half to 60 timeout changes.
60 void startWithLazyStop();
61
62 void stop();
63
64 void invalidate();
65
66 // Return true if stop() was not called betfore the responsiveness timeout.
67 bool isResponsive() const { return m_isResponsive; }
68
69 // Return true if there is an active timer. The state could be responsive or not.
70 bool hasActiveTimer() const { return m_waitingForTimer; }
71
72 void processTerminated();
73
74private:
75 void timerFired();
76
77 ResponsivenessTimer::Client& m_client;
78
79 RunLoop::Timer<ResponsivenessTimer> m_timer;
80 MonotonicTime m_restartFireTime;
81
82 bool m_isResponsive { true };
83 bool m_waitingForTimer { false };
84 bool m_useLazyStop { false };
85};
86
87} // namespace WebKit
88
89#endif // ResponsivenessTimer_h
90
91