Web Inspector: "Reload Web Inspector" button no longer partially works
[WebKit-https.git] / Source / WebKit / UIProcess / ProcessThrottler.cpp
1 /*
2  * Copyright (C) 2014 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 "ProcessThrottler.h"
28
29 #include "Logging.h"
30 #include "ProcessThrottlerClient.h"
31
32 namespace WebKit {
33     
34 static const Seconds processSuspensionTimeout { 30_s };
35     
36 ProcessThrottler::ProcessThrottler(ProcessThrottlerClient& process, bool shouldTakeUIBackgroundAssertion)
37     : m_process(process)
38     , m_suspendTimer(RunLoop::main(), this, &ProcessThrottler::suspendTimerFired)
39     , m_foregroundCounter([this](RefCounterEvent) { updateAssertion(); })
40     , m_backgroundCounter([this](RefCounterEvent) { updateAssertion(); })
41     , m_shouldTakeUIBackgroundAssertion(shouldTakeUIBackgroundAssertion)
42 {
43 }
44     
45 AssertionState ProcessThrottler::assertionState()
46 {
47     ASSERT(!m_suspendTimer.isActive());
48     
49     if (m_foregroundCounter.value())
50         return AssertionState::Foreground;
51     if (m_backgroundCounter.value())
52         return AssertionState::Background;
53     return AssertionState::Suspended;
54 }
55     
56 void ProcessThrottler::updateAssertionNow()
57 {
58     m_suspendTimer.stop();
59     if (m_assertion) {
60         auto newState = assertionState();
61         if (m_assertion->state() == newState)
62             return;
63         RELEASE_LOG(ProcessSuspension, "%p - ProcessThrottler::updateAssertionNow() updating process assertion state to %u (foregroundActivities: %lu, backgroundActivities: %lu)", this, newState, m_foregroundCounter.value(), m_backgroundCounter.value());
64         m_assertion->setState(newState);
65         m_process.didSetAssertionState(newState);
66     }
67 }
68     
69 void ProcessThrottler::updateAssertion()
70 {
71     // If the process is currently runnable but will be suspended then first give it a chance to complete what it was doing
72     // and clean up - move it to the background and send it a message to notify. Schedule a timeout so it can't stay running
73     // in the background for too long.
74     if (m_assertion && m_assertion->state() != AssertionState::Suspended && !m_foregroundCounter.value() && !m_backgroundCounter.value()) {
75         ++m_suspendMessageCount;
76         RELEASE_LOG(ProcessSuspension, "%p - ProcessThrottler::updateAssertion() sending PrepareToSuspend IPC", this);
77         m_process.sendPrepareToSuspend();
78         m_suspendTimer.startOneShot(processSuspensionTimeout);
79         m_assertion->setState(AssertionState::Background);
80         m_process.didSetAssertionState(AssertionState::Background);
81         return;
82     }
83
84     bool shouldBeRunnable = m_foregroundCounter.value() || m_backgroundCounter.value();
85
86     // If we're currently waiting for the Web process to do suspension cleanup, but no longer need to be suspended, tell the Web process to cancel the cleanup.
87     if (m_suspendTimer.isActive() && shouldBeRunnable)
88         m_process.sendCancelPrepareToSuspend();
89     
90     if (m_assertion && m_assertion->state() == AssertionState::Suspended && shouldBeRunnable)
91         m_process.sendProcessDidResume();
92
93     updateAssertionNow();
94 }
95
96 void ProcessThrottler::didConnectToProcess(ProcessID pid)
97 {
98     RELEASE_LOG(ProcessSuspension, "%p - ProcessThrottler::didConnectToProcess(%d)", this, pid);
99
100     m_suspendTimer.stop();
101     if (m_shouldTakeUIBackgroundAssertion)
102         m_assertion = std::make_unique<ProcessAndUIAssertion>(pid, assertionState());
103     else
104         m_assertion = std::make_unique<ProcessAssertion>(pid, assertionState());
105     m_process.didSetAssertionState(assertionState());
106     m_assertion->setClient(*this);
107 }
108     
109 void ProcessThrottler::suspendTimerFired()
110 {
111     updateAssertionNow();
112 }
113     
114 void ProcessThrottler::processReadyToSuspend()
115 {
116     if (!--m_suspendMessageCount)
117         updateAssertionNow();
118     ASSERT(m_suspendMessageCount >= 0);
119 }
120
121 void ProcessThrottler::didCancelProcessSuspension()
122 {
123     if (!--m_suspendMessageCount)
124         updateAssertionNow();
125     ASSERT(m_suspendMessageCount >= 0);
126 }
127
128 void ProcessThrottler::assertionWillExpireImminently()
129 {
130     m_process.sendProcessWillSuspendImminently();
131 }
132
133 }