PSON logic gets confused by concurrent decidePolicyForNavigationAction requests
[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_isSuspended)
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     if (!m_finishedSuspending)
118         m_readyToUnsuspendHandler = WTFMove(completionHandler);
119     else
120         completionHandler(this);
121 }
122
123 void SuspendedPageProxy::unsuspend()
124 {
125     ASSERT(m_isSuspended);
126     ASSERT(m_finishedSuspending);
127
128     m_isSuspended = false;
129     m_process->removeMessageReceiver(Messages::WebPageProxy::messageReceiverName(), m_page.pageID());
130     m_process->send(Messages::WebPage::SetIsSuspended(false), m_page.pageID());
131 }
132
133 void SuspendedPageProxy::didFinishLoad()
134 {
135     LOG(ProcessSwapping, "SuspendedPageProxy %s from process %i finished transition to suspended", loggingString(), m_process->processIdentifier());
136
137     m_finishedSuspending = true;
138
139     m_process->send(Messages::WebProcess::UpdateActivePages(), 0);
140
141 #if PLATFORM(IOS_FAMILY)
142     m_suspensionToken = nullptr;
143 #endif
144
145     if (m_readyToUnsuspendHandler)
146         m_readyToUnsuspendHandler(this);
147 }
148
149 void SuspendedPageProxy::didFailToSuspend()
150 {
151     // We are unusable due to failure to suspend so remove ourselves from the WebProcessPool.
152     auto protectedThis = m_process->processPool().takeSuspendedPage(*this);
153
154     if (m_readyToUnsuspendHandler)
155         m_readyToUnsuspendHandler(nullptr);
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::DidFinishLoadForFrame::name()) {
163         didFinishLoad();
164         return;
165     }
166
167     if (decoder.messageName() == Messages::WebPageProxy::DidFailToSuspendAfterProcessSwap::name()) {
168         didFailToSuspend();
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_finishedSuspending ", String::number(m_finishedSuspending), ")");
186 }
187 #endif
188
189 } // namespace WebKit