f198a81533f76b2e8e2bf71d4a3b9d2740c95078
[WebKit-https.git] / Source / WebKit / UIProcess / SuspendedPageProxy.cpp
1 /*
2  * Copyright (C) 2018 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 "SuspendedPageProxy.h"
28
29 #include "Logging.h"
30 #include "WebPageMessages.h"
31 #include "WebPageProxy.h"
32 #include "WebPageProxyMessages.h"
33 #include "WebProcessMessages.h"
34 #include "WebProcessPool.h"
35 #include "WebProcessProxy.h"
36 #include <wtf/DebugUtilities.h>
37 #include <wtf/URL.h>
38
39 namespace WebKit {
40 using namespace WebCore;
41
42 #if !LOG_DISABLED
43 static const HashSet<IPC::StringReference>& messageNamesToIgnoreWhileSuspended()
44 {
45     static NeverDestroyed<HashSet<IPC::StringReference>> messageNames;
46     static std::once_flag onceFlag;
47     std::call_once(onceFlag, [] {
48         messageNames.get().add("BackForwardAddItem");
49         messageNames.get().add("ClearAllEditCommands");
50         messageNames.get().add("DidChangeContentSize");
51         messageNames.get().add("DidChangeMainDocument");
52         messageNames.get().add("DidChangeProgress");
53         messageNames.get().add("DidCommitLoadForFrame");
54         messageNames.get().add("DidDestroyNavigation");
55         messageNames.get().add("DidFinishDocumentLoadForFrame");
56         messageNames.get().add("DidFinishProgress");
57         messageNames.get().add("DidCompletePageTransition");
58         messageNames.get().add("DidFirstLayoutForFrame");
59         messageNames.get().add("DidFirstVisuallyNonEmptyLayoutForFrame");
60         messageNames.get().add("DidNavigateWithNavigationData");
61         messageNames.get().add("DidReachLayoutMilestone");
62         messageNames.get().add("DidRestoreScrollPosition");
63         messageNames.get().add("DidSaveToPageCache");
64         messageNames.get().add("DidStartProgress");
65         messageNames.get().add("DidStartProvisionalLoadForFrame");
66         messageNames.get().add("EditorStateChanged");
67         messageNames.get().add("PageExtendedBackgroundColorDidChange");
68         messageNames.get().add("SetRenderTreeSize");
69         messageNames.get().add("SetStatusText");
70         messageNames.get().add("SetNetworkRequestsInProgress");
71     });
72
73     return messageNames;
74 }
75 #endif
76
77 SuspendedPageProxy::SuspendedPageProxy(WebPageProxy& page, Ref<WebProcessProxy>&& process, WebBackForwardListItem& item, uint64_t mainFrameID)
78     : m_page(page)
79     , m_process(WTFMove(process))
80     , m_mainFrameID(mainFrameID)
81     , m_registrableDomain(toRegistrableDomain(URL(URL(), item.url())))
82 #if PLATFORM(IOS_FAMILY)
83     , m_suspensionToken(m_process->throttler().backgroundActivityToken())
84 #endif
85 {
86     item.setSuspendedPage(this);
87     m_process->addMessageReceiver(Messages::WebPageProxy::messageReceiverName(), m_page.pageID(), *this);
88
89     m_process->send(Messages::WebPage::SetIsSuspended(true), m_page.pageID());
90 }
91
92 SuspendedPageProxy::~SuspendedPageProxy()
93 {
94     if (m_readyToUnsuspendHandler)
95         m_readyToUnsuspendHandler(nullptr);
96
97     if (m_suspensionState == SuspensionState::Resumed)
98         return;
99
100     // If the suspended page was not consumed before getting destroyed, then close the corresponding page
101     // on the WebProcess side.
102     m_process->send(Messages::WebPage::Close(), m_page.pageID());
103     m_process->removeMessageReceiver(Messages::WebPageProxy::messageReceiverName(), m_page.pageID());
104
105     // We call maybeShutDown() asynchronously since the SuspendedPage is currently being removed from the WebProcessPool
106     // and we want to avoid re-entering WebProcessPool methods.
107     RunLoop::main().dispatch([process = m_process.copyRef()] {
108         process->maybeShutDown();
109     });
110 }
111
112 void SuspendedPageProxy::waitUntilReadyToUnsuspend(CompletionHandler<void(SuspendedPageProxy*)>&& completionHandler)
113 {
114     if (m_readyToUnsuspendHandler)
115         m_readyToUnsuspendHandler(nullptr);
116
117     switch (m_suspensionState) {
118     case SuspensionState::Suspending:
119         m_readyToUnsuspendHandler = WTFMove(completionHandler);
120         break;
121     case SuspensionState::FailedToSuspend:
122     case SuspensionState::Suspended:
123         completionHandler(this);
124         break;
125     case SuspensionState::Resumed:
126         ASSERT_NOT_REACHED();
127         completionHandler(nullptr);
128         break;
129     }
130 }
131
132 void SuspendedPageProxy::unsuspend()
133 {
134     ASSERT(m_suspensionState == SuspensionState::Suspended);
135
136     m_suspensionState = SuspensionState::Resumed;
137     m_process->removeMessageReceiver(Messages::WebPageProxy::messageReceiverName(), m_page.pageID());
138     m_process->send(Messages::WebPage::SetIsSuspended(false), m_page.pageID());
139 }
140
141 void SuspendedPageProxy::didProcessRequestToSuspend(SuspensionState newSuspensionState)
142 {
143     LOG(ProcessSwapping, "SuspendedPageProxy %s from process %i finished transition to suspended", loggingString(), m_process->processIdentifier());
144
145     ASSERT(m_suspensionState == SuspensionState::Suspending);
146     ASSERT(newSuspensionState == SuspensionState::Suspended || newSuspensionState == SuspensionState::FailedToSuspend);
147
148     m_suspensionState = newSuspensionState;
149
150 #if PLATFORM(IOS_FAMILY)
151     m_suspensionToken = nullptr;
152 #endif
153
154     if (m_readyToUnsuspendHandler)
155         m_readyToUnsuspendHandler(this);
156 }
157
158 void SuspendedPageProxy::didReceiveMessage(IPC::Connection&, IPC::Decoder& decoder)
159 {
160     ASSERT(decoder.messageReceiverName() == Messages::WebPageProxy::messageReceiverName());
161
162     if (decoder.messageName() == Messages::WebPageProxy::DidSuspendAfterProcessSwap::name()) {
163         didProcessRequestToSuspend(SuspensionState::Suspended);
164         return;
165     }
166
167     if (decoder.messageName() == Messages::WebPageProxy::DidFailToSuspendAfterProcessSwap::name()) {
168         didProcessRequestToSuspend(SuspensionState::FailedToSuspend);
169         return;
170     }
171
172 #if !LOG_DISABLED
173     if (!messageNamesToIgnoreWhileSuspended().contains(decoder.messageName()))
174         LOG(ProcessSwapping, "SuspendedPageProxy received unexpected WebPageProxy message '%s'", decoder.messageName().toString().data());
175 #endif
176 }
177
178 void SuspendedPageProxy::didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&)
179 {
180 }
181
182 #if !LOG_DISABLED
183 const char* SuspendedPageProxy::loggingString() const
184 {
185     return debugString("(", String::format("%p", this), " page ID ", String::number(m_page.pageID()), ", m_suspensionState ", String::number(static_cast<unsigned>(m_suspensionState)), ")");
186 }
187 #endif
188
189 } // namespace WebKit