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 | #ifndef ProcessThrottler_h |
27 | #define ProcessThrottler_h |
28 | |
29 | #include "ProcessAssertion.h" |
30 | |
31 | #include <wtf/ProcessID.h> |
32 | #include <wtf/RefCounter.h> |
33 | #include <wtf/RunLoop.h> |
34 | #include <wtf/WeakPtr.h> |
35 | |
36 | namespace WebKit { |
37 | |
38 | enum UserObservablePageCounterType { }; |
39 | typedef RefCounter<UserObservablePageCounterType> UserObservablePageCounter; |
40 | enum ProcessSuppressionDisabledCounterType { }; |
41 | typedef RefCounter<ProcessSuppressionDisabledCounterType> ProcessSuppressionDisabledCounter; |
42 | typedef ProcessSuppressionDisabledCounter::Token ProcessSuppressionDisabledToken; |
43 | |
44 | class ProcessThrottlerClient; |
45 | |
46 | class ProcessThrottler : private ProcessAssertion::Client { |
47 | public: |
48 | enum ForegroundActivityCounterType { }; |
49 | typedef RefCounter<ForegroundActivityCounterType> ForegroundActivityCounter; |
50 | typedef ForegroundActivityCounter::Token ForegroundActivityToken; |
51 | enum BackgroundActivityCounterType { }; |
52 | typedef RefCounter<BackgroundActivityCounterType> BackgroundActivityCounter; |
53 | typedef BackgroundActivityCounter::Token BackgroundActivityToken; |
54 | |
55 | ProcessThrottler(ProcessThrottlerClient&, bool shouldTakeUIBackgroundAssertion); |
56 | |
57 | inline ForegroundActivityToken foregroundActivityToken() const; |
58 | inline BackgroundActivityToken backgroundActivityToken() const; |
59 | |
60 | void didConnectToProcess(ProcessID); |
61 | void processReadyToSuspend(); |
62 | void didCancelProcessSuspension(); |
63 | bool shouldBeRunnable() const { return m_foregroundCounter.value() || m_backgroundCounter.value(); } |
64 | |
65 | private: |
66 | AssertionState assertionState(); |
67 | void updateAssertion(); |
68 | void updateAssertionNow(); |
69 | void suspendTimerFired(); |
70 | |
71 | // ProcessAssertionClient |
72 | void uiAssertionWillExpireImminently() override; |
73 | |
74 | ProcessThrottlerClient& m_process; |
75 | std::unique_ptr<ProcessAssertion> m_assertion; |
76 | RunLoop::Timer<ProcessThrottler> m_suspendTimer; |
77 | ForegroundActivityCounter m_foregroundCounter; |
78 | BackgroundActivityCounter m_backgroundCounter; |
79 | int m_suspendMessageCount { 0 }; |
80 | bool m_shouldTakeUIBackgroundAssertion; |
81 | bool m_uiAssertionExpired { false }; |
82 | }; |
83 | |
84 | inline ProcessThrottler::ForegroundActivityToken ProcessThrottler::foregroundActivityToken() const |
85 | { |
86 | return ForegroundActivityToken(m_foregroundCounter.count()); |
87 | } |
88 | |
89 | inline ProcessThrottler::BackgroundActivityToken ProcessThrottler::backgroundActivityToken() const |
90 | { |
91 | return BackgroundActivityToken(m_backgroundCounter.count()); |
92 | } |
93 | |
94 | } |
95 | |
96 | #endif // ProcessThrottler_h |
97 | |