[iOS] Replace "node assistance" terminology in WebKit with "focused element"
[WebKit-https.git] / Source / WebKit / UIProcess / ProcessAssertion.h
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 ProcessAssertion_h
27 #define ProcessAssertion_h
28
29 #include <wtf/Function.h>
30 #include <wtf/ProcessID.h>
31
32 #if !OS(WINDOWS)
33 #include <unistd.h>
34 #endif
35
36 #if PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)
37 #include <wtf/RetainPtr.h>
38 #include <wtf/WeakPtr.h>
39 OBJC_CLASS BKSProcessAssertion;
40 #endif
41
42 namespace WebKit {
43     
44 enum class AssertionState {
45     Suspended,
46     Background,
47     Foreground
48 };
49
50 class ProcessAssertionClient {
51 public:
52     virtual ~ProcessAssertionClient() { };
53     virtual void assertionWillExpireImminently() = 0;
54 };
55
56 class ProcessAssertion
57 #if PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)
58     : public CanMakeWeakPtr<ProcessAssertion>
59 #endif
60 {
61 public:
62     ProcessAssertion(ProcessID, AssertionState, Function<void()>&& invalidationCallback = { });
63     virtual ~ProcessAssertion();
64
65     virtual void setClient(ProcessAssertionClient& client) { m_client = &client; }
66     ProcessAssertionClient* client() { return m_client; }
67
68     AssertionState state() const { return m_assertionState; }
69     virtual void setState(AssertionState);
70
71 #if PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)
72 protected:
73     enum class Validity { No, Yes, Unset };
74     Validity validity() const { return m_validity; }
75 #endif
76
77 private:
78 #if PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)
79     void markAsInvalidated();
80
81     RetainPtr<BKSProcessAssertion> m_assertion;
82     Validity m_validity { Validity::Unset };
83     Function<void()> m_invalidationCallback;
84 #endif
85     AssertionState m_assertionState;
86     ProcessAssertionClient* m_client { nullptr };
87 };
88     
89 class ProcessAndUIAssertion final : public ProcessAssertion {
90 public:
91     ProcessAndUIAssertion(ProcessID, AssertionState);
92     ~ProcessAndUIAssertion();
93
94     void setClient(ProcessAssertionClient&) final;
95
96     void setState(AssertionState) final;
97
98 #if PLATFORM(IOS_FAMILY) && !PLATFORM(IOS_FAMILY_SIMULATOR)
99 private:
100     void updateRunInBackgroundCount();
101
102     bool m_isHoldingBackgroundAssertion { false };
103 #endif
104 };
105     
106 }
107
108 #endif // ProcessAssertion_h