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 "WebProcessProxy.h"
30
31namespace WebKit {
32
33static const unsigned processSuspensionTimeout = 30;
34
35ProcessThrottler::ForegroundActivityToken::ForegroundActivityToken(ProcessThrottler& throttler)
36    : m_throttler(throttler.weakPtr())
37{
38    throttler.m_foregroundCount++;
39    throttler.updateAssertion();
40}
41
42ProcessThrottler::ForegroundActivityToken::~ForegroundActivityToken()
43{
44    if (ProcessThrottler* throttler = m_throttler.get()) {
45        throttler->m_foregroundCount--;
46        throttler->updateAssertion();
47    }
48}
49
50ProcessThrottler::BackgroundActivityToken::BackgroundActivityToken(ProcessThrottler& throttler)
51    : m_throttler(throttler.weakPtr())
52{
53    throttler.m_backgroundCount++;
54    throttler.updateAssertion();
55}
56
57ProcessThrottler::BackgroundActivityToken::~BackgroundActivityToken()
58{
59    if (ProcessThrottler* throttler = m_throttler.get()) {
60        throttler->m_backgroundCount--;
61        throttler->updateAssertion();
62    }
63}
64
65ProcessThrottler::ProcessThrottler(WebProcessProxy* process)
66    : m_process(process)
67    , m_weakPtrFactory(this)
68    , m_suspendTimer(RunLoop::main(), this, &ProcessThrottler::suspendTimerFired)
69    , m_foregroundCount(0)
70    , m_backgroundCount(0)
71    , m_suspendMessageCount(0)
72{
73}
74
75AssertionState ProcessThrottler::assertionState()
76{
77    ASSERT(!m_suspendTimer.isActive());
78
79    if (m_foregroundCount)
80        return AssertionState::Foreground;
81    if (m_backgroundCount)
82        return AssertionState::Background;
83    return AssertionState::Suspended;
84}
85
86void ProcessThrottler::updateAssertionNow()
87{
88    m_suspendTimer.stop();
89    if (m_assertion)
90        m_assertion->setState(assertionState());
91}
92
93void ProcessThrottler::updateAssertion()
94{
95    // If the process is currently runnable but will be suspended then first give it a chance to complete what it was doing
96    // and clean up - move it to the background and send it a message to notify. Schedule a timeout so it can't stay running
97    // in the background for too long.
98    if (m_assertion && m_assertion->state() != AssertionState::Suspended && !m_foregroundCount && !m_backgroundCount) {
99        ++m_suspendMessageCount;
100        m_process->sendProcessWillSuspend();
101        m_suspendTimer.startOneShot(processSuspensionTimeout);
102        m_assertion->setState(AssertionState::Background);
103        return;
104    }
105
106    // 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.
107    if (m_suspendTimer.isActive() && (m_foregroundCount || m_backgroundCount))
108        m_process->sendCancelProcessWillSuspend();
109
110    updateAssertionNow();
111}
112
113void ProcessThrottler::didConnnectToProcess(pid_t pid)
114{
115    m_suspendTimer.stop();
116    m_assertion = std::make_unique<ProcessAndUIAssertion>(pid, assertionState());
117}
118
119void ProcessThrottler::suspendTimerFired()
120{
121    updateAssertionNow();
122}
123
124void ProcessThrottler::processReadyToSuspend()
125{
126    if (!--m_suspendMessageCount)
127        updateAssertionNow();
128    ASSERT(m_suspendMessageCount >= 0);
129}
130
131void ProcessThrottler::didCancelProcessSuspension()
132{
133    if (!--m_suspendMessageCount)
134        updateAssertionNow();
135    ASSERT(m_suspendMessageCount >= 0);
136}
137
138}
139