1 | /* |
2 | * Copyright (C) 2010, 2012 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 | #include "config.h" |
27 | #include "ResponsivenessTimer.h" |
28 | |
29 | namespace WebKit { |
30 | |
31 | static const Seconds responsivenessTimeout { 3_s }; |
32 | |
33 | ResponsivenessTimer::ResponsivenessTimer(ResponsivenessTimer::Client& client) |
34 | : m_client(client) |
35 | , m_timer(RunLoop::main(), this, &ResponsivenessTimer::timerFired) |
36 | { |
37 | } |
38 | |
39 | ResponsivenessTimer::~ResponsivenessTimer() = default; |
40 | |
41 | void ResponsivenessTimer::invalidate() |
42 | { |
43 | m_timer.stop(); |
44 | m_restartFireTime = MonotonicTime(); |
45 | m_waitingForTimer = false; |
46 | m_useLazyStop = false; |
47 | } |
48 | |
49 | void ResponsivenessTimer::timerFired() |
50 | { |
51 | if (!m_waitingForTimer) |
52 | return; |
53 | |
54 | if (m_restartFireTime) { |
55 | MonotonicTime now = MonotonicTime::now(); |
56 | MonotonicTime restartFireTime = m_restartFireTime; |
57 | m_restartFireTime = MonotonicTime(); |
58 | |
59 | if (restartFireTime > now) { |
60 | m_timer.startOneShot(now - restartFireTime); |
61 | return; |
62 | } |
63 | } |
64 | |
65 | m_waitingForTimer = false; |
66 | m_useLazyStop = false; |
67 | |
68 | if (!m_isResponsive) |
69 | return; |
70 | |
71 | if (!m_client.mayBecomeUnresponsive()) { |
72 | m_waitingForTimer = true; |
73 | m_timer.startOneShot(responsivenessTimeout); |
74 | return; |
75 | } |
76 | |
77 | m_client.willChangeIsResponsive(); |
78 | m_isResponsive = false; |
79 | m_client.didChangeIsResponsive(); |
80 | |
81 | m_client.didBecomeUnresponsive(); |
82 | } |
83 | |
84 | void ResponsivenessTimer::start() |
85 | { |
86 | if (m_waitingForTimer) |
87 | return; |
88 | |
89 | m_waitingForTimer = true; |
90 | m_useLazyStop = false; |
91 | |
92 | if (m_timer.isActive()) { |
93 | // The timer is still active from a lazy stop. |
94 | // Instead of restarting the timer, we schedule a new delay after this one finishes. |
95 | // |
96 | // In most cases, stop is called before we get to schedule the second timer, saving us |
97 | // the scheduling of the timer entirely. |
98 | m_restartFireTime = MonotonicTime::now() + responsivenessTimeout; |
99 | } else { |
100 | m_restartFireTime = MonotonicTime(); |
101 | m_timer.startOneShot(responsivenessTimeout); |
102 | } |
103 | } |
104 | |
105 | void ResponsivenessTimer::startWithLazyStop() |
106 | { |
107 | if (!m_waitingForTimer) { |
108 | start(); |
109 | m_useLazyStop = true; |
110 | } |
111 | } |
112 | |
113 | void ResponsivenessTimer::stop() |
114 | { |
115 | if (!m_isResponsive) { |
116 | // We got a life sign from the web process. |
117 | m_client.willChangeIsResponsive(); |
118 | m_isResponsive = true; |
119 | m_client.didChangeIsResponsive(); |
120 | |
121 | m_client.didBecomeResponsive(); |
122 | } |
123 | |
124 | m_waitingForTimer = false; |
125 | |
126 | if (m_useLazyStop) |
127 | m_useLazyStop = false; |
128 | else |
129 | m_timer.stop(); |
130 | } |
131 | |
132 | void ResponsivenessTimer::processTerminated() |
133 | { |
134 | invalidate(); |
135 | } |
136 | |
137 | } // namespace WebKit |
138 | |