[Cocoa] WKCustomProtocolLoader should store a WeakPtr to its LegacyCustomProtocolMana...
[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     bool shouldBeRunnable = this->shouldBeRunnable();
72
73     // If the process is currently runnable but will be suspended then first give it a chance to complete what it was doing
74     // and clean up - move it to the background and send it a message to notify. Schedule a timeout so it can't stay running
75     // in the background for too long.
76     if (m_assertion && m_assertion->state() != AssertionState::Suspended && !shouldBeRunnable) {
77         ++m_suspendMessageCount;
78         RELEASE_LOG(ProcessSuspension, "%p - ProcessThrottler::updateAssertion() sending PrepareToSuspend IPC", this);
79         m_process.sendPrepareToSuspend();
80         m_suspendTimer.startOneShot(processSuspensionTimeout);
81         m_assertion->setState(AssertionState::Background);
82         m_process.didSetAssertionState(AssertionState::Background);
83         return;
84     }
85
86     if (shouldBeRunnable) {
87         // 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.
88         if (m_suspendTimer.isActive())
89             m_process.sendCancelPrepareToSuspend();
90
91         if ((m_assertion && m_assertion->state() == AssertionState::Suspended) || m_uiAssertionExpired) {
92             m_process.sendProcessDidResume();
93             m_uiAssertionExpired = false;
94         }
95     }
96
97     updateAssertionNow();
98 }
99
100 void ProcessThrottler::didConnectToProcess(ProcessID pid)
101 {
102     RELEASE_LOG(ProcessSuspension, "%p - ProcessThrottler::didConnectToProcess(%d)", this, pid);
103
104     m_suspendTimer.stop();
105     if (m_shouldTakeUIBackgroundAssertion)
106         m_assertion = std::make_unique<ProcessAndUIAssertion>(pid, "Web content visibility"_s, assertionState());
107     else
108         m_assertion = std::make_unique<ProcessAssertion>(pid, "Web content visibility"_s, assertionState());
109     m_process.didSetAssertionState(assertionState());
110     m_assertion->setClient(*this);
111 }
112     
113 void ProcessThrottler::suspendTimerFired()
114 {
115     updateAssertionNow();
116 }
117     
118 void ProcessThrottler::processReadyToSuspend()
119 {
120     if (!--m_suspendMessageCount)
121         updateAssertionNow();
122     ASSERT(m_suspendMessageCount >= 0);
123 }
124
125 void ProcessThrottler::didCancelProcessSuspension()
126 {
127     if (!--m_suspendMessageCount)
128         updateAssertionNow();
129     ASSERT(m_suspendMessageCount >= 0);
130 }
131
132 void ProcessThrottler::uiAssertionWillExpireImminently()
133 {
134     m_process.sendProcessWillSuspendImminently();
135     m_uiAssertionExpired = true;
136 }
137
138 }