a3f5711b051ee1f494e0f7ca7fbe89659f1cc434
[WebKit-https.git] / Source / WebKit / UIProcess / ProvisionalPageProxy.cpp
1 /*
2  * Copyright (C) 2019 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 "ProvisionalPageProxy.h"
28
29 #include "APINavigation.h"
30 #include "DrawingAreaProxy.h"
31 #include "FormDataReference.h"
32 #include "Logging.h"
33 #include "PageClient.h"
34 #include "URLSchemeTaskParameters.h"
35 #include "WebBackForwardList.h"
36 #include "WebBackForwardListItem.h"
37 #include "WebErrors.h"
38 #include "WebNavigationDataStore.h"
39 #include "WebNavigationState.h"
40 #include "WebPageMessages.h"
41 #include "WebPageProxy.h"
42 #include "WebPageProxyMessages.h"
43 #include "WebProcessMessages.h"
44 #include "WebProcessProxy.h"
45 #include <WebCore/ShouldTreatAsContinuingLoad.h>
46
47 namespace WebKit {
48
49 #define RELEASE_LOG_IF_ALLOWED(channel, fmt, ...) RELEASE_LOG_IF(m_page.isAlwaysOnLoggingAllowed(), channel, "%p - ProvisionalPageProxy::" fmt, this, ##__VA_ARGS__)
50 #define RELEASE_LOG_ERROR_IF_ALLOWED(channel, fmt, ...) RELEASE_LOG_ERROR_IF(m_page.isAlwaysOnLoggingAllowed(), channel, "%p - ProvisionalPageProxy::" fmt, this, ##__VA_ARGS__)
51
52 ProvisionalPageProxy::ProvisionalPageProxy(WebPageProxy& page, Ref<WebProcessProxy>&& process, std::unique_ptr<SuspendedPageProxy> suspendedPage, uint64_t navigationID, bool isServerRedirect, const WebCore::ResourceRequest& request, ProcessSwapRequestedByClient processSwapRequestedByClient)
53     : m_page(page)
54     , m_process(WTFMove(process))
55     , m_navigationID(navigationID)
56     , m_isServerRedirect(isServerRedirect)
57     , m_request(request)
58     , m_processSwapRequestedByClient(processSwapRequestedByClient)
59 #if PLATFORM(IOS_FAMILY)
60     , m_suspensionToken(m_process->throttler().foregroundActivityToken())
61 #endif
62 {
63     m_process->addMessageReceiver(Messages::WebPageProxy::messageReceiverName(), m_page.pageID(), *this);
64     m_process->addProvisionalPageProxy(*this);
65
66     // If we are reattaching to a SuspendedPage, then the WebProcess' WebPage already exists and
67     // WebPageProxy::didCreateMainFrame() will not be called to initialize m_mainFrame. In such
68     // case, we need to initialize m_mainFrame to reflect the fact the the WebProcess' WebPage
69     // already exists and already has a main frame.
70     if (suspendedPage) {
71         ASSERT(&suspendedPage->process() == m_process.ptr());
72         suspendedPage->unsuspend();
73         m_mainFrame = WebFrameProxy::create(m_page, suspendedPage->mainFrameID());
74         m_process->frameCreated(suspendedPage->mainFrameID(), *m_mainFrame);
75     }
76
77     initializeWebPage();
78 }
79
80 ProvisionalPageProxy::~ProvisionalPageProxy()
81 {
82     m_process->removeProvisionalPageProxy(*this);
83
84     if (m_wasCommitted)
85         return;
86
87     m_process->removeMessageReceiver(Messages::WebPageProxy::messageReceiverName(), m_page.pageID());
88     m_process->send(Messages::WebPage::Close(), m_page.pageID());
89
90     RunLoop::main().dispatch([process = m_process.copyRef()] {
91         process->maybeShutDown();
92     });
93 }
94
95 void ProvisionalPageProxy::processDidTerminate()
96 {
97     RELEASE_LOG_ERROR_IF_ALLOWED(ProcessSwapping, "processDidTerminate: pageID = %" PRIu64, m_page.pageID());
98     m_page.provisionalProcessDidTerminate();
99 }
100
101 std::unique_ptr<DrawingAreaProxy> ProvisionalPageProxy::takeDrawingArea()
102 {
103     return WTFMove(m_drawingArea);
104 }
105
106 void ProvisionalPageProxy::cancel()
107 {
108     // If the provisional load started, then indicate that it failed due to cancellation by calling didFailProvisionalLoadForFrame().
109     if (m_provisionalLoadURL.isEmpty())
110         return;
111
112     RELEASE_LOG_IF_ALLOWED(ProcessSwapping, "cancel: Simulating a didFailProvisionalLoadForFrame for pageID = %" PRIu64, m_page.pageID());
113     ASSERT(m_mainFrame);
114     auto error = WebKit::cancelledError(m_request);
115     error.setType(WebCore::ResourceError::Type::Cancellation);
116     didFailProvisionalLoadForFrame(m_mainFrame->frameID(), { }, m_navigationID, m_provisionalLoadURL, error, UserData { }); // Will delete |this|.
117 }
118
119 void ProvisionalPageProxy::processDidFinishLaunching()
120 {
121     RELEASE_LOG_IF_ALLOWED(ProcessSwapping, "processDidFinishLaunching: pageID = %" PRIu64, m_page.pageID());
122     finishInitializingWebPageAfterProcessLaunch();
123 }
124
125 void ProvisionalPageProxy::finishInitializingWebPageAfterProcessLaunch()
126 {
127     ASSERT(m_process->state() == WebProcessProxy::State::Running);
128
129     // FIXME: The WebPageProxy delays adding the visited link store until after the process has launched
130     // so the ProvisionalPageProxy does the same. However, do we really need to?
131     m_process->addVisitedLinkStore(m_page.visitedLinkStore());
132 }
133
134 void ProvisionalPageProxy::initializeWebPage()
135 {
136     m_drawingArea = m_page.pageClient().createDrawingAreaProxy(m_process);
137
138     auto parameters = m_page.creationParameters(m_process);
139     parameters.isProcessSwap = true;
140     m_process->send(Messages::WebProcess::CreateWebPage(m_page.pageID(), parameters), 0);
141
142     if (m_process->state() == WebProcessProxy::State::Running)
143         finishInitializingWebPageAfterProcessLaunch();
144 }
145
146 void ProvisionalPageProxy::loadData(API::Navigation& navigation, const IPC::DataReference& data, const String& MIMEType, const String& encoding, const String& baseURL, API::Object* userData, Optional<WebsitePoliciesData>&& websitePolicies)
147 {
148     RELEASE_LOG_IF_ALLOWED(ProcessSwapping, "loadData: pageID = %" PRIu64, m_page.pageID());
149
150     m_page.loadDataWithNavigationShared(m_process.copyRef(), navigation, data, MIMEType, encoding, baseURL, userData, WebCore::ShouldTreatAsContinuingLoad::Yes, WTFMove(websitePolicies));
151 }
152
153 void ProvisionalPageProxy::loadRequest(API::Navigation& navigation, WebCore::ResourceRequest&& request, WebCore::ShouldOpenExternalURLsPolicy shouldOpenExternalURLsPolicy, API::Object* userData, Optional<WebsitePoliciesData>&& websitePolicies)
154 {
155     RELEASE_LOG_IF_ALLOWED(ProcessSwapping, "loadRequest: pageID = %" PRIu64, m_page.pageID());
156
157     m_page.loadRequestWithNavigationShared(m_process.copyRef(), navigation, WTFMove(request), shouldOpenExternalURLsPolicy, userData, WebCore::ShouldTreatAsContinuingLoad::Yes, WTFMove(websitePolicies));
158 }
159
160 void ProvisionalPageProxy::goToBackForwardItem(API::Navigation& navigation, WebBackForwardListItem& item, Optional<WebsitePoliciesData>&& websitePolicies)
161 {
162     RELEASE_LOG_IF_ALLOWED(ProcessSwapping, "goToBackForwardItem: pageID = %" PRIu64, m_page.pageID());
163
164     auto itemStates = m_page.backForwardList().filteredItemStates([this, targetItem = &item](auto& item) {
165         if (auto* page = item.suspendedPage()) {
166             if (&page->process() == m_process.ptr())
167                 return false;
168         }
169         return &item != targetItem;
170     });
171     m_process->send(Messages::WebPage::UpdateBackForwardListForReattach(WTFMove(itemStates)), m_page.pageID());
172     m_process->send(Messages::WebPage::GoToBackForwardItem(navigation.navigationID(), item.itemID(), *navigation.backForwardFrameLoadType(), WebCore::ShouldTreatAsContinuingLoad::Yes, WTFMove(websitePolicies)), m_page.pageID());
173     m_process->responsivenessTimer().start();
174 }
175
176 void ProvisionalPageProxy::didCreateMainFrame(uint64_t frameID)
177 {
178     RELEASE_LOG_IF_ALLOWED(ProcessSwapping, "didCreateMainFrame: pageID = %" PRIu64 ", frameID = %" PRIu64, m_page.pageID(), frameID);
179     ASSERT(!m_mainFrame);
180
181     m_mainFrame = WebFrameProxy::create(m_page, frameID);
182
183     // Add the frame to the process wide map.
184     m_process->frameCreated(frameID, *m_mainFrame);
185
186     // This navigation was destroyed so no need to notify of redirect.
187     if (!m_page.navigationState().hasNavigation(m_navigationID))
188         return;
189
190     // Restore the main frame's committed URL as some clients may rely on it until the next load is committed.
191     if (auto* mainFrame = m_page.mainFrame())
192         m_mainFrame->frameLoadState().setURL(mainFrame->frameLoadState().url());
193
194     // Normally, notification of a server redirect comes from the WebContent process.
195     // If we are process swapping in response to a server redirect then that notification will not come from the new WebContent process.
196     // In this case we have the UIProcess synthesize the redirect notification at the appropriate time.
197     if (m_isServerRedirect) {
198         m_mainFrame->frameLoadState().didStartProvisionalLoad(m_request.url());
199         m_page.didReceiveServerRedirectForProvisionalLoadForFrameShared(m_process.copyRef(), m_mainFrame->frameID(), m_navigationID, WTFMove(m_request), { });
200     }
201 }
202
203 void ProvisionalPageProxy::didPerformClientRedirect(const String& sourceURLString, const String& destinationURLString, uint64_t frameID)
204 {
205     m_page.didPerformClientRedirectShared(m_process.copyRef(), sourceURLString, destinationURLString, frameID);
206 }
207
208 void ProvisionalPageProxy::didStartProvisionalLoadForFrame(uint64_t frameID, uint64_t navigationID, URL&& url, URL&& unreachableURL, const UserData& userData)
209 {
210     // If the previous provisional load used the same process, we may receive IPC for this previous provisional's main frame that we need to ignore.
211     if (!m_mainFrame || m_mainFrame->frameID() != frameID)
212         return;
213
214     RELEASE_LOG_IF_ALLOWED(ProcessSwapping, "didStartProvisionalLoadForFrame: pageID = %" PRIu64 ", frameID = %" PRIu64 ", navigationID = %" PRIu64, m_page.pageID(), frameID, navigationID);
215     ASSERT(m_provisionalLoadURL.isNull());
216     m_provisionalLoadURL = url;
217
218     // Merely following a server side redirect so there is no need to send a didStartProvisionalLoad again.
219     if (m_isServerRedirect)
220         return;
221
222     // Clients expect the Page's main frame's expectedURL to be the provisional one when a provisional load is started.
223     if (auto* pageMainFrame = m_page.mainFrame())
224         pageMainFrame->didStartProvisionalLoad(url);
225
226     m_page.didStartProvisionalLoadForFrameShared(m_process.copyRef(), frameID, navigationID, WTFMove(url), WTFMove(unreachableURL), userData);
227 }
228
229 void ProvisionalPageProxy::didFailProvisionalLoadForFrame(uint64_t frameID, const WebCore::SecurityOriginData& frameSecurityOrigin, uint64_t navigationID, const String& provisionalURL, const WebCore::ResourceError& error, const UserData& userData)
230 {
231     RELEASE_LOG_ERROR_IF_ALLOWED(ProcessSwapping, "didFailProvisionalLoadForFrame: pageID = %" PRIu64 ", frameID = %" PRIu64 ", navigationID = %" PRIu64, m_page.pageID(), frameID, navigationID);
232     m_provisionalLoadURL = { };
233
234     // Make sure the Page's main frame's expectedURL gets cleared since we updated it in didStartProvisionalLoad.
235     if (auto* pageMainFrame = m_page.mainFrame())
236         pageMainFrame->didFailProvisionalLoad();
237
238     m_page.didFailProvisionalLoadForFrameShared(m_process.copyRef(), frameID, frameSecurityOrigin, navigationID, provisionalURL, error, userData); // Will delete |this|.
239 }
240
241 void ProvisionalPageProxy::didCommitLoadForFrame(uint64_t frameID, uint64_t navigationID, const String& mimeType, bool frameHasCustomContentProvider, uint32_t frameLoadType, const WebCore::CertificateInfo& certificateInfo, bool containsPluginDocument, Optional<WebCore::HasInsecureContent> forcedHasInsecureContent, const UserData& userData)
242 {
243     // If the previous provisional load used the same process, we may receive IPC for this previous provisional's main frame that we need to ignore.
244     if (!m_mainFrame || m_mainFrame->frameID() != frameID)
245         return;
246
247     RELEASE_LOG_IF_ALLOWED(ProcessSwapping, "didCommitLoadForFrame: pageID = %" PRIu64 ", frameID = %" PRIu64 ", navigationID = %" PRIu64, m_page.pageID(), frameID, navigationID);
248     m_provisionalLoadURL = { };
249     m_process->removeMessageReceiver(Messages::WebPageProxy::messageReceiverName(), m_page.pageID());
250
251     m_wasCommitted = true;
252     m_page.commitProvisionalPage(frameID, navigationID, mimeType, frameHasCustomContentProvider, frameLoadType, certificateInfo, containsPluginDocument, forcedHasInsecureContent, userData); // Will delete |this|.
253 }
254
255 void ProvisionalPageProxy::didNavigateWithNavigationData(const WebNavigationDataStore& store, uint64_t frameID)
256 {
257     m_page.didNavigateWithNavigationDataShared(m_process.copyRef(), store, frameID);
258 }
259
260 void ProvisionalPageProxy::didChangeProvisionalURLForFrame(uint64_t frameID, uint64_t navigationID, URL&& url)
261 {
262     m_page.didChangeProvisionalURLForFrameShared(m_process.copyRef(), frameID, navigationID, WTFMove(url));
263 }
264
265 void ProvisionalPageProxy::decidePolicyForNavigationActionAsync(uint64_t frameID, WebCore::SecurityOriginData&& frameSecurityOrigin, WebCore::PolicyCheckIdentifier identifier,
266     uint64_t navigationID, NavigationActionData&& navigationActionData, FrameInfoData&& frameInfoData, uint64_t originatingPageID, const WebCore::ResourceRequest& originalRequest,
267     WebCore::ResourceRequest&& request, IPC::FormDataReference&& requestBody, WebCore::ResourceResponse&& redirectResponse, const UserData& userData, uint64_t listenerID)
268 {
269     ASSERT(m_mainFrame);
270     ASSERT(m_mainFrame->frameID() == frameID);
271     m_page.decidePolicyForNavigationActionAsyncShared(m_process.copyRef(), frameID, WTFMove(frameSecurityOrigin), identifier, navigationID, WTFMove(navigationActionData),
272         WTFMove(frameInfoData), originatingPageID, originalRequest, WTFMove(request), WTFMove(requestBody), WTFMove(redirectResponse), userData, listenerID);
273 }
274
275 void ProvisionalPageProxy::decidePolicyForResponse(uint64_t frameID, const WebCore::SecurityOriginData& frameSecurityOrigin, WebCore::PolicyCheckIdentifier identifier,
276     uint64_t navigationID, const WebCore::ResourceResponse& response, const WebCore::ResourceRequest& request, bool canShowMIMEType, uint64_t listenerID, const UserData& userData)
277 {
278     m_page.decidePolicyForResponseShared(m_process.copyRef(), frameID, frameSecurityOrigin, identifier, navigationID, response, request, canShowMIMEType, listenerID, userData);
279 }
280
281 void ProvisionalPageProxy::startURLSchemeTask(URLSchemeTaskParameters&& parameters)
282 {
283     m_page.startURLSchemeTaskShared(m_process.copyRef(), WTFMove(parameters));
284 }
285
286 void ProvisionalPageProxy::backForwardGoToItem(const WebCore::BackForwardItemIdentifier& identifier, SandboxExtension::Handle& handle)
287 {
288     m_page.backForwardGoToItemShared(m_process.copyRef(), identifier, handle);
289 }
290
291 #if PLATFORM(COCOA)
292 void ProvisionalPageProxy::registerWebProcessAccessibilityToken(const IPC::DataReference& data)
293 {
294     m_accessibilityToken = data.vector();
295 }
296 #endif
297
298 void ProvisionalPageProxy::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
299 {
300     ASSERT(decoder.messageReceiverName() == Messages::WebPageProxy::messageReceiverName());
301
302     if (decoder.messageName() == Messages::WebPageProxy::DidStartProgress::name()
303         || decoder.messageName() == Messages::WebPageProxy::DidChangeProgress::name()
304         || decoder.messageName() == Messages::WebPageProxy::BackForwardAddItem::name()
305         || decoder.messageName() == Messages::WebPageProxy::LogDiagnosticMessage::name()
306         || decoder.messageName() == Messages::WebPageProxy::LogDiagnosticMessageWithEnhancedPrivacy::name()
307         || decoder.messageName() == Messages::WebPageProxy::SetNetworkRequestsInProgress::name()
308         )
309     {
310         m_page.didReceiveMessage(connection, decoder);
311         return;
312     }
313
314 #if PLATFORM(COCOA)
315     if (decoder.messageName() == Messages::WebPageProxy::RegisterWebProcessAccessibilityToken::name()) {
316         IPC::handleMessage<Messages::WebPageProxy::RegisterWebProcessAccessibilityToken>(decoder, this, &ProvisionalPageProxy::registerWebProcessAccessibilityToken);
317         return;
318     }
319 #endif
320
321     if (decoder.messageName() == Messages::WebPageProxy::StartURLSchemeTask::name()) {
322         IPC::handleMessage<Messages::WebPageProxy::StartURLSchemeTask>(decoder, this, &ProvisionalPageProxy::startURLSchemeTask);
323         return;
324     }
325
326     if (decoder.messageName() == Messages::WebPageProxy::DecidePolicyForNavigationActionAsync::name()) {
327         IPC::handleMessage<Messages::WebPageProxy::DecidePolicyForNavigationActionAsync>(decoder, this, &ProvisionalPageProxy::decidePolicyForNavigationActionAsync);
328         return;
329     }
330
331     if (decoder.messageName() == Messages::WebPageProxy::DecidePolicyForResponse::name()) {
332         IPC::handleMessage<Messages::WebPageProxy::DecidePolicyForResponse>(decoder, this, &ProvisionalPageProxy::decidePolicyForResponse);
333         return;
334     }
335
336     if (decoder.messageName() == Messages::WebPageProxy::DidChangeProvisionalURLForFrame::name()) {
337         IPC::handleMessage<Messages::WebPageProxy::DidChangeProvisionalURLForFrame>(decoder, this, &ProvisionalPageProxy::didChangeProvisionalURLForFrame);
338         return;
339     }
340
341     if (decoder.messageName() == Messages::WebPageProxy::DidNavigateWithNavigationData::name()) {
342         IPC::handleMessage<Messages::WebPageProxy::DidNavigateWithNavigationData>(decoder, this, &ProvisionalPageProxy::didNavigateWithNavigationData);
343         return;
344     }
345
346     if (decoder.messageName() == Messages::WebPageProxy::DidPerformClientRedirect::name()) {
347         IPC::handleMessage<Messages::WebPageProxy::DidPerformClientRedirect>(decoder, this, &ProvisionalPageProxy::didPerformClientRedirect);
348         return;
349     }
350
351     if (decoder.messageName() == Messages::WebPageProxy::DidCreateMainFrame::name()) {
352         IPC::handleMessage<Messages::WebPageProxy::DidCreateMainFrame>(decoder, this, &ProvisionalPageProxy::didCreateMainFrame);
353         return;
354     }
355
356     if (decoder.messageName() == Messages::WebPageProxy::DidStartProvisionalLoadForFrame::name()) {
357         IPC::handleMessage<Messages::WebPageProxy::DidStartProvisionalLoadForFrame>(decoder, this, &ProvisionalPageProxy::didStartProvisionalLoadForFrame);
358         return;
359     }
360
361     if (decoder.messageName() == Messages::WebPageProxy::DidFailProvisionalLoadForFrame::name()) {
362         IPC::handleMessage<Messages::WebPageProxy::DidFailProvisionalLoadForFrame>(decoder, this, &ProvisionalPageProxy::didFailProvisionalLoadForFrame);
363         return;
364     }
365
366     if (decoder.messageName() == Messages::WebPageProxy::DidCommitLoadForFrame::name()) {
367         IPC::handleMessage<Messages::WebPageProxy::DidCommitLoadForFrame>(decoder, this, &ProvisionalPageProxy::didCommitLoadForFrame);
368         return;
369     }
370
371     LOG(ProcessSwapping, "Unhandled message %s::%s from provisional process", decoder.messageReceiverName().toString().data(), decoder.messageName().toString().data());
372 }
373
374 void ProvisionalPageProxy::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
375 {
376     if (decoder.messageName() == Messages::WebPageProxy::BackForwardGoToItem::name()) {
377         IPC::handleMessageLegacySync<Messages::WebPageProxy::BackForwardGoToItem>(decoder, *replyEncoder, this, &ProvisionalPageProxy::backForwardGoToItem);
378         return;
379     }
380
381     m_page.didReceiveSyncMessage(connection, decoder, replyEncoder);
382 }
383
384 } // namespace WebKit