Add API::InjectedBundle::ResourceLoadClient
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebCoreSupport / WebFrameLoaderClient.cpp
1 /*
2  * Copyright (C) 2010-2017 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 "WebFrameLoaderClient.h"
28
29 #include "AuthenticationManager.h"
30 #include "DataReference.h"
31 #include "DrawingArea.h"
32 #include "FindController.h"
33 #include "InjectedBundle.h"
34 #include "InjectedBundleBackForwardListItem.h"
35 #include "InjectedBundleDOMWindowExtension.h"
36 #include "InjectedBundleNavigationAction.h"
37 #include "NavigationActionData.h"
38 #include "PluginView.h"
39 #include "UserData.h"
40 #include "WKBundleAPICast.h"
41 #include "WebAutomationSessionProxy.h"
42 #include "WebBackForwardListProxy.h"
43 #include "WebCoreArgumentCoders.h"
44 #include "WebDocumentLoader.h"
45 #include "WebErrors.h"
46 #include "WebEvent.h"
47 #include "WebFrame.h"
48 #include "WebFrameNetworkingContext.h"
49 #include "WebFullScreenManager.h"
50 #include "WebIconDatabaseMessages.h"
51 #include "WebNavigationDataStore.h"
52 #include "WebPage.h"
53 #include "WebPageGroupProxy.h"
54 #include "WebPageProxyMessages.h"
55 #include "WebProcess.h"
56 #include "WebProcessPoolMessages.h"
57 #include "WebsitePolicies.h"
58 #include <JavaScriptCore/APICast.h>
59 #include <JavaScriptCore/JSObject.h>
60 #include <WebCore/CachedFrame.h>
61 #include <WebCore/CertificateInfo.h>
62 #include <WebCore/Chrome.h>
63 #include <WebCore/DOMWrapperWorld.h>
64 #include <WebCore/DocumentLoader.h>
65 #include <WebCore/FormState.h>
66 #include <WebCore/FrameLoadRequest.h>
67 #include <WebCore/FrameLoader.h>
68 #include <WebCore/FrameView.h>
69 #include <WebCore/HTMLAppletElement.h>
70 #include <WebCore/HTMLFormElement.h>
71 #include <WebCore/HistoryController.h>
72 #include <WebCore/HistoryItem.h>
73 #include <WebCore/MIMETypeRegistry.h>
74 #include <WebCore/MainFrame.h>
75 #include <WebCore/MouseEvent.h>
76 #include <WebCore/NotImplemented.h>
77 #include <WebCore/Page.h>
78 #include <WebCore/PluginData.h>
79 #include <WebCore/PluginDocument.h>
80 #include <WebCore/ProgressTracker.h>
81 #include <WebCore/ResourceError.h>
82 #include <WebCore/ScriptController.h>
83 #include <WebCore/SecurityOriginData.h>
84 #include <WebCore/Settings.h>
85 #include <WebCore/SubframeLoader.h>
86 #include <WebCore/UIEventWithKeyState.h>
87 #include <WebCore/Widget.h>
88 #include <WebCore/WindowFeatures.h>
89 #include <wtf/NeverDestroyed.h>
90
91 using namespace WebCore;
92
93 namespace WebKit {
94
95 WebFrameLoaderClient::WebFrameLoaderClient()
96     : m_frame(0)
97     , m_hasSentResponseToPluginView(false)
98     , m_didCompletePageTransition(false)
99     , m_frameHasCustomContentProvider(false)
100     , m_frameCameFromPageCache(false)
101 {
102 }
103
104 WebFrameLoaderClient::~WebFrameLoaderClient()
105 {
106 }
107     
108 void WebFrameLoaderClient::frameLoaderDestroyed()
109 {
110     m_frame->invalidate();
111
112     // Balances explicit ref() in WebFrame::create().
113     m_frame->deref();
114 }
115
116 bool WebFrameLoaderClient::hasHTMLView() const
117 {
118     return !m_frameHasCustomContentProvider;
119 }
120
121 bool WebFrameLoaderClient::hasWebView() const
122 {
123     return m_frame->page();
124 }
125
126 void WebFrameLoaderClient::makeRepresentation(DocumentLoader*)
127 {
128     notImplemented();
129 }
130
131 void WebFrameLoaderClient::forceLayoutForNonHTML()
132 {
133     notImplemented();
134 }
135
136 void WebFrameLoaderClient::setCopiesOnScroll()
137 {
138     notImplemented();
139 }
140
141 void WebFrameLoaderClient::detachedFromParent2()
142 {
143     WebPage* webPage = m_frame->page();
144     if (!webPage)
145         return;
146
147     RefPtr<API::Object> userData;
148
149     // Notify the bundle client.
150     webPage->injectedBundleLoaderClient().didRemoveFrameFromHierarchy(*webPage, *m_frame, userData);
151 }
152
153 void WebFrameLoaderClient::detachedFromParent3()
154 {
155     notImplemented();
156 }
157
158 void WebFrameLoaderClient::assignIdentifierToInitialRequest(unsigned long identifier, DocumentLoader* loader, const ResourceRequest& request)
159 {
160     WebPage* webPage = m_frame->page();
161     if (!webPage)
162         return;
163
164     bool pageIsProvisionallyLoading = false;
165     if (FrameLoader* frameLoader = loader->frameLoader())
166         pageIsProvisionallyLoading = frameLoader->provisionalDocumentLoader() == loader;
167
168     webPage->injectedBundleResourceLoadClient().didInitiateLoadForResource(*webPage, *m_frame, identifier, request, pageIsProvisionallyLoading);
169     webPage->addResourceRequest(identifier, request);
170 }
171
172 void WebFrameLoaderClient::dispatchWillSendRequest(DocumentLoader*, unsigned long identifier, ResourceRequest& request, const ResourceResponse& redirectResponse)
173 {
174     WebPage* webPage = m_frame->page();
175     if (!webPage)
176         return;
177
178     webPage->injectedBundleResourceLoadClient().willSendRequestForFrame(*webPage, *m_frame, identifier, request, redirectResponse);
179 }
180
181 bool WebFrameLoaderClient::shouldUseCredentialStorage(DocumentLoader*, unsigned long identifier)
182 {
183     WebPage* webPage = m_frame->page();
184     if (!webPage)
185         return true;
186
187     return webPage->injectedBundleResourceLoadClient().shouldUseCredentialStorage(*webPage, *m_frame, identifier);
188 }
189
190 void WebFrameLoaderClient::dispatchDidReceiveAuthenticationChallenge(DocumentLoader*, unsigned long, const AuthenticationChallenge& challenge)
191 {
192     // FIXME: Authentication is a per-resource concept, but we don't do per-resource handling in the UIProcess at the API level quite yet.
193     // Once we do, we might need to make sure authentication fits with our solution.
194
195     WebPage* webPage = m_frame->page();
196     if (!webPage)
197         return;
198
199     WebProcess::singleton().supplement<AuthenticationManager>()->didReceiveAuthenticationChallenge(m_frame, challenge);
200 }
201
202 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
203 bool WebFrameLoaderClient::canAuthenticateAgainstProtectionSpace(DocumentLoader*, unsigned long, const ProtectionSpace& protectionSpace)
204 {
205     // The WebKit 2 Networking process asks the UIProcess directly, so the WebContent process should never receive this callback.
206     ASSERT_NOT_REACHED();
207     return false;
208 }
209 #endif
210
211 void WebFrameLoaderClient::dispatchDidReceiveResponse(DocumentLoader*, unsigned long identifier, const ResourceResponse& response)
212 {
213     WebPage* webPage = m_frame->page();
214     if (!webPage)
215         return;
216
217     webPage->injectedBundleResourceLoadClient().didReceiveResponseForResource(*webPage, *m_frame, identifier, response);
218 }
219
220 void WebFrameLoaderClient::dispatchDidReceiveContentLength(DocumentLoader*, unsigned long identifier, int dataLength)
221 {
222     WebPage* webPage = m_frame->page();
223     if (!webPage)
224         return;
225
226     webPage->injectedBundleResourceLoadClient().didReceiveContentLengthForResource(*webPage, *m_frame, identifier, dataLength);
227 }
228
229 #if ENABLE(DATA_DETECTION)
230 void WebFrameLoaderClient::dispatchDidFinishDataDetection(NSArray *detectionResults)
231 {
232     WebPage* webPage = m_frame->page();
233     if (!webPage)
234         return;
235     webPage->setDataDetectionResults(detectionResults);
236 }
237 #endif
238
239 void WebFrameLoaderClient::dispatchDidFinishLoading(DocumentLoader*, unsigned long identifier)
240 {
241     WebPage* webPage = m_frame->page();
242     if (!webPage)
243         return;
244
245     webPage->injectedBundleResourceLoadClient().didFinishLoadForResource(*webPage, *m_frame, identifier);
246     webPage->removeResourceRequest(identifier);
247 }
248
249 void WebFrameLoaderClient::dispatchDidFailLoading(DocumentLoader*, unsigned long identifier, const ResourceError& error)
250 {
251     WebPage* webPage = m_frame->page();
252     if (!webPage)
253         return;
254
255     webPage->injectedBundleResourceLoadClient().didFailLoadForResource(*webPage, *m_frame, identifier, error);
256     webPage->removeResourceRequest(identifier);
257 }
258
259 bool WebFrameLoaderClient::dispatchDidLoadResourceFromMemoryCache(DocumentLoader*, const ResourceRequest&, const ResourceResponse&, int /*length*/)
260 {
261     notImplemented();
262     return false;
263 }
264
265 void WebFrameLoaderClient::dispatchDidDispatchOnloadEvents()
266 {
267     WebPage* webPage = m_frame->page();
268     if (!webPage)
269         return;
270
271     // Notify the bundle client.
272     webPage->injectedBundleLoaderClient().didHandleOnloadEventsForFrame(*webPage, *m_frame);
273 }
274
275 void WebFrameLoaderClient::dispatchDidReceiveServerRedirectForProvisionalLoad()
276 {
277     WebPage* webPage = m_frame->page();
278     if (!webPage)
279         return;
280
281     WebDocumentLoader& documentLoader = static_cast<WebDocumentLoader&>(*m_frame->coreFrame()->loader().provisionalDocumentLoader());
282     const String& url = documentLoader.url().string();
283     RefPtr<API::Object> userData;
284
285     // Notify the bundle client.
286     webPage->injectedBundleLoaderClient().didReceiveServerRedirectForProvisionalLoadForFrame(*webPage, *m_frame, userData);
287
288     // Notify the UIProcess.
289     webPage->send(Messages::WebPageProxy::DidReceiveServerRedirectForProvisionalLoadForFrame(m_frame->frameID(), documentLoader.navigationID(), url, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
290 }
291
292 void WebFrameLoaderClient::dispatchDidChangeProvisionalURL()
293 {
294     WebPage* webPage = m_frame->page();
295     if (!webPage)
296         return;
297
298     WebDocumentLoader& documentLoader = static_cast<WebDocumentLoader&>(*m_frame->coreFrame()->loader().provisionalDocumentLoader());
299     webPage->send(Messages::WebPageProxy::DidChangeProvisionalURLForFrame(m_frame->frameID(), documentLoader.navigationID(), documentLoader.url().string()));
300 }
301
302 void WebFrameLoaderClient::dispatchDidCancelClientRedirect()
303 {
304     WebPage* webPage = m_frame->page();
305     if (!webPage)
306         return;
307
308     // Notify the bundle client.
309     webPage->injectedBundleLoaderClient().didCancelClientRedirectForFrame(*webPage, *m_frame);
310 }
311
312 void WebFrameLoaderClient::dispatchWillPerformClientRedirect(const URL& url, double interval, double fireDate)
313 {
314     WebPage* webPage = m_frame->page();
315     if (!webPage)
316         return;
317
318     // Notify the bundle client.
319     webPage->injectedBundleLoaderClient().willPerformClientRedirectForFrame(*webPage, *m_frame, url, interval, fireDate);
320 }
321
322 void WebFrameLoaderClient::dispatchDidChangeLocationWithinPage()
323 {
324     WebPage* webPage = m_frame->page();
325     if (!webPage)
326         return;
327
328     RefPtr<API::Object> userData;
329
330     auto navigationID = static_cast<WebDocumentLoader&>(*m_frame->coreFrame()->loader().documentLoader()).navigationID();
331
332     // Notify the bundle client.
333     webPage->injectedBundleLoaderClient().didSameDocumentNavigationForFrame(*webPage, *m_frame, SameDocumentNavigationAnchorNavigation, userData);
334
335     // Notify the UIProcess.
336     webPage->send(Messages::WebPageProxy::DidSameDocumentNavigationForFrame(m_frame->frameID(), navigationID, SameDocumentNavigationAnchorNavigation, m_frame->coreFrame()->document()->url().string(), UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
337 }
338
339 void WebFrameLoaderClient::dispatchDidChangeMainDocument()
340 {
341     WebPage* webPage = m_frame->page();
342     if (!webPage)
343         return;
344
345     webPage->send(Messages::WebPageProxy::DidChangeMainDocument(m_frame->frameID()));
346 }
347
348 void WebFrameLoaderClient::dispatchDidPushStateWithinPage()
349 {
350     WebPage* webPage = m_frame->page();
351     if (!webPage)
352         return;
353
354     RefPtr<API::Object> userData;
355
356     auto navigationID = static_cast<WebDocumentLoader&>(*m_frame->coreFrame()->loader().documentLoader()).navigationID();
357
358     // Notify the bundle client.
359     webPage->injectedBundleLoaderClient().didSameDocumentNavigationForFrame(*webPage, *m_frame, SameDocumentNavigationSessionStatePush, userData);
360
361     // Notify the UIProcess.
362     webPage->send(Messages::WebPageProxy::DidSameDocumentNavigationForFrame(m_frame->frameID(), navigationID, SameDocumentNavigationSessionStatePush, m_frame->coreFrame()->document()->url().string(), UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
363 }
364
365 void WebFrameLoaderClient::dispatchDidReplaceStateWithinPage()
366 {
367     WebPage* webPage = m_frame->page();
368     if (!webPage)
369         return;
370
371     RefPtr<API::Object> userData;
372
373     auto navigationID = static_cast<WebDocumentLoader&>(*m_frame->coreFrame()->loader().documentLoader()).navigationID();
374
375     // Notify the bundle client.
376     webPage->injectedBundleLoaderClient().didSameDocumentNavigationForFrame(*webPage, *m_frame, SameDocumentNavigationSessionStateReplace, userData);
377
378     // Notify the UIProcess.
379     webPage->send(Messages::WebPageProxy::DidSameDocumentNavigationForFrame(m_frame->frameID(), navigationID, SameDocumentNavigationSessionStateReplace, m_frame->coreFrame()->document()->url().string(), UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
380 }
381
382 void WebFrameLoaderClient::dispatchDidPopStateWithinPage()
383 {
384     WebPage* webPage = m_frame->page();
385     if (!webPage)
386         return;
387
388     RefPtr<API::Object> userData;
389
390     auto navigationID = static_cast<WebDocumentLoader&>(*m_frame->coreFrame()->loader().documentLoader()).navigationID();
391
392     // Notify the bundle client.
393     webPage->injectedBundleLoaderClient().didSameDocumentNavigationForFrame(*webPage, *m_frame, SameDocumentNavigationSessionStatePop, userData);
394
395     // Notify the UIProcess.
396     webPage->send(Messages::WebPageProxy::DidSameDocumentNavigationForFrame(m_frame->frameID(), navigationID, SameDocumentNavigationSessionStatePop, m_frame->coreFrame()->document()->url().string(), UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
397 }
398
399 void WebFrameLoaderClient::dispatchWillClose()
400 {
401     notImplemented();
402 }
403
404 void WebFrameLoaderClient::dispatchDidReceiveIcon()
405 {
406     WebProcess::singleton().parentProcessConnection()->send(Messages::WebIconDatabase::DidReceiveIconForPageURL(m_frame->url()), 0);
407 }
408
409 void WebFrameLoaderClient::dispatchDidStartProvisionalLoad()
410 {
411     WebPage* webPage = m_frame->page();
412     if (!webPage)
413         return;
414
415 #if ENABLE(FULLSCREEN_API)
416     Element* documentElement = m_frame->coreFrame()->document()->documentElement();
417     if (documentElement && documentElement->containsFullScreenElement())
418         webPage->fullScreenManager()->exitFullScreenForElement(webPage->fullScreenManager()->element());
419 #endif
420
421     webPage->findController().hideFindUI();
422     webPage->sandboxExtensionTracker().didStartProvisionalLoad(m_frame);
423
424     WebDocumentLoader& provisionalLoader = static_cast<WebDocumentLoader&>(*m_frame->coreFrame()->loader().provisionalDocumentLoader());
425     const String& url = provisionalLoader.url().string();
426     RefPtr<API::Object> userData;
427
428     // Notify the bundle client.
429     webPage->injectedBundleLoaderClient().didStartProvisionalLoadForFrame(*webPage, *m_frame, userData);
430
431     String unreachableURL = provisionalLoader.unreachableURL().string();
432
433     // Notify the UIProcess.
434     webPage->send(Messages::WebPageProxy::DidStartProvisionalLoadForFrame(m_frame->frameID(), provisionalLoader.navigationID(), url, unreachableURL, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
435 }
436
437 static constexpr unsigned maxTitleLength = 1000; // Closest power of 10 above the W3C recommendation for Title length.
438
439 void WebFrameLoaderClient::dispatchDidReceiveTitle(const StringWithDirection& title)
440 {
441     WebPage* webPage = m_frame->page();
442     if (!webPage)
443         return;
444
445     auto truncatedTitle = truncateFromEnd(title, maxTitleLength);
446     
447     RefPtr<API::Object> userData;
448
449     // Notify the bundle client.
450     // FIXME: Use direction of title.
451     webPage->injectedBundleLoaderClient().didReceiveTitleForFrame(*webPage, truncatedTitle.string, *m_frame, userData);
452
453     // Notify the UIProcess.
454     webPage->send(Messages::WebPageProxy::DidReceiveTitleForFrame(m_frame->frameID(), truncatedTitle.string, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
455 }
456
457 void WebFrameLoaderClient::dispatchDidCommitLoad(std::optional<HasInsecureContent> hasInsecureContent)
458 {
459     WebPage* webPage = m_frame->page();
460     if (!webPage)
461         return;
462
463     WebDocumentLoader& documentLoader = static_cast<WebDocumentLoader&>(*m_frame->coreFrame()->loader().documentLoader());
464     RefPtr<API::Object> userData;
465
466     // Notify the bundle client.
467     webPage->injectedBundleLoaderClient().didCommitLoadForFrame(*webPage, *m_frame, userData);
468
469     webPage->sandboxExtensionTracker().didCommitProvisionalLoad(m_frame);
470
471     // Notify the UIProcess.
472     webPage->send(Messages::WebPageProxy::DidCommitLoadForFrame(m_frame->frameID(), documentLoader.navigationID(), documentLoader.response().mimeType(), m_frameHasCustomContentProvider, static_cast<uint32_t>(m_frame->coreFrame()->loader().loadType()), valueOrCompute(documentLoader.response().certificateInfo(), [] { return CertificateInfo(); }), m_frame->coreFrame()->document()->isPluginDocument(), hasInsecureContent, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
473     webPage->didCommitLoad(m_frame);
474 }
475
476 void WebFrameLoaderClient::dispatchDidFailProvisionalLoad(const ResourceError& error)
477 {
478     WebPage* webPage = m_frame->page();
479     if (!webPage)
480         return;
481
482     RefPtr<API::Object> userData;
483
484     // Notify the bundle client.
485     webPage->injectedBundleLoaderClient().didFailProvisionalLoadWithErrorForFrame(*webPage, *m_frame, error, userData);
486
487     webPage->sandboxExtensionTracker().didFailProvisionalLoad(m_frame);
488
489     // FIXME: This is gross. This is necessary because if the client calls WKBundlePageStopLoading() from within the didFailProvisionalLoadWithErrorForFrame
490     // injected bundle client call, that will cause the provisional DocumentLoader to be disconnected from the Frame, and didDistroyNavigation message
491     // to be sent to the UIProcess (and the destruction of the DocumentLoader). If that happens, and we had captured the navigationID before injected bundle 
492     // client call, the DidFailProvisionalLoadForFrame would send a navigationID of a destroyed Navigation, and the UIProcess would not be able to find it
493     // in its table.
494     //
495     // A better solution to this problem would be find a clean way to postpone the disconnection of the DocumentLoader from the Frame until
496     // the entire FrameLoaderClient function was complete.
497     uint64_t navigationID = 0;
498     if (auto documentLoader = m_frame->coreFrame()->loader().provisionalDocumentLoader())
499         navigationID = static_cast<WebDocumentLoader*>(documentLoader)->navigationID();
500
501     // Notify the UIProcess.
502     WebCore::Frame* coreFrame = m_frame ? m_frame->coreFrame() : nullptr;
503     webPage->send(Messages::WebPageProxy::DidFailProvisionalLoadForFrame(m_frame->frameID(), SecurityOriginData::fromFrame(coreFrame), navigationID, m_frame->coreFrame()->loader().provisionalLoadErrorBeingHandledURL(), error, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
504
505     // If we have a load listener, notify it.
506     if (WebFrame::LoadListener* loadListener = m_frame->loadListener())
507         loadListener->didFailLoad(m_frame, error.isCancellation());
508 }
509
510 void WebFrameLoaderClient::dispatchDidFailLoad(const ResourceError& error)
511 {
512     WebPage* webPage = m_frame->page();
513     if (!webPage)
514         return;
515
516     RefPtr<API::Object> userData;
517
518     auto navigationID = static_cast<WebDocumentLoader&>(*m_frame->coreFrame()->loader().documentLoader()).navigationID();
519
520     // Notify the bundle client.
521     webPage->injectedBundleLoaderClient().didFailLoadWithErrorForFrame(*webPage, *m_frame, error, userData);
522
523     // Notify the UIProcess.
524     webPage->send(Messages::WebPageProxy::DidFailLoadForFrame(m_frame->frameID(), navigationID, error, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
525
526     // If we have a load listener, notify it.
527     if (WebFrame::LoadListener* loadListener = m_frame->loadListener())
528         loadListener->didFailLoad(m_frame, error.isCancellation());
529 }
530
531 void WebFrameLoaderClient::dispatchDidFinishDocumentLoad()
532 {
533     WebPage* webPage = m_frame->page();
534     if (!webPage)
535         return;
536
537     RefPtr<API::Object> userData;
538
539     auto navigationID = static_cast<WebDocumentLoader&>(*m_frame->coreFrame()->loader().documentLoader()).navigationID();
540
541     // Notify the bundle client.
542     webPage->injectedBundleLoaderClient().didFinishDocumentLoadForFrame(*webPage, *m_frame, userData);
543
544     // Notify the UIProcess.
545     webPage->send(Messages::WebPageProxy::DidFinishDocumentLoadForFrame(m_frame->frameID(), navigationID, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
546 }
547
548 void WebFrameLoaderClient::dispatchDidFinishLoad()
549 {
550     WebPage* webPage = m_frame->page();
551     if (!webPage)
552         return;
553
554     RefPtr<API::Object> userData;
555
556     auto navigationID = static_cast<WebDocumentLoader&>(*m_frame->coreFrame()->loader().documentLoader()).navigationID();
557
558     // Notify the bundle client.
559     webPage->injectedBundleLoaderClient().didFinishLoadForFrame(*webPage, *m_frame, userData);
560
561     // Notify the UIProcess.
562     webPage->send(Messages::WebPageProxy::DidFinishLoadForFrame(m_frame->frameID(), navigationID, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
563
564     // If we have a load listener, notify it.
565     if (WebFrame::LoadListener* loadListener = m_frame->loadListener())
566         loadListener->didFinishLoad(m_frame);
567
568     webPage->didFinishLoad(m_frame);
569 }
570
571 void WebFrameLoaderClient::forcePageTransitionIfNeeded()
572 {
573     if (m_didCompletePageTransition)
574         return;
575
576     WebPage* webPage = m_frame->page();
577     if (!webPage)
578         return;
579
580     webPage->didCompletePageTransition();
581     m_didCompletePageTransition = true;
582 }
583
584 void WebFrameLoaderClient::dispatchDidReachLayoutMilestone(LayoutMilestones milestones)
585 {
586     WebPage* webPage = m_frame->page();
587     if (!webPage)
588         return;
589
590     RefPtr<API::Object> userData;
591
592     if (milestones & DidFirstLayout) {
593         // FIXME: We should consider removing the old didFirstLayout API since this is doing double duty with the
594         // new didLayout API.
595         webPage->injectedBundleLoaderClient().didFirstLayoutForFrame(*webPage, *m_frame, userData);
596         webPage->send(Messages::WebPageProxy::DidFirstLayoutForFrame(m_frame->frameID(), UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
597
598 #if PLATFORM(MAC)
599         // FIXME: Do this on DidFirstVisuallyNonEmptyLayout when Mac Safari is able to handle it (<rdar://problem/17580021>)
600         if (m_frame->isMainFrame() && !m_didCompletePageTransition && !webPage->corePage()->settings().suppressesIncrementalRendering()) {
601             webPage->didCompletePageTransition();
602             m_didCompletePageTransition = true;
603         }
604 #endif
605
606 #if USE(COORDINATED_GRAPHICS)
607         // Make sure viewport properties are dispatched on the main frame by the time the first layout happens.
608         ASSERT(!webPage->useFixedLayout() || m_frame != m_frame->page()->mainWebFrame() || m_frame->coreFrame()->document()->didDispatchViewportPropertiesChanged());
609 #endif
610     }
611
612     // Send this after DidFirstLayout-specific calls since some clients expect to get those messages first.
613     webPage->dispatchDidReachLayoutMilestone(milestones);
614
615     if (milestones & DidFirstVisuallyNonEmptyLayout) {
616         if (m_frame->isMainFrame() && !m_didCompletePageTransition && !webPage->corePage()->settings().suppressesIncrementalRendering()) {
617             webPage->didCompletePageTransition();
618             m_didCompletePageTransition = true;
619         }
620
621         // FIXME: We should consider removing the old didFirstVisuallyNonEmptyLayoutForFrame API since this is doing
622         // double duty with the new didLayout API.
623         webPage->injectedBundleLoaderClient().didFirstVisuallyNonEmptyLayoutForFrame(*webPage, *m_frame, userData);
624         webPage->send(Messages::WebPageProxy::DidFirstVisuallyNonEmptyLayoutForFrame(m_frame->frameID(), UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
625     }
626 }
627
628 void WebFrameLoaderClient::dispatchDidLayout()
629 {
630     WebPage* webPage = m_frame->page();
631     if (!webPage)
632         return;
633
634     // Notify the bundle client.
635     webPage->injectedBundleLoaderClient().didLayoutForFrame(*webPage, *m_frame);
636
637     webPage->recomputeShortCircuitHorizontalWheelEventsState();
638
639 #if PLATFORM(IOS)
640     webPage->updateSelectionAppearance();
641 #endif
642
643     // NOTE: Unlike the other layout notifications, this does not notify the
644     // the UIProcess for every call.
645
646     if (m_frame == m_frame->page()->mainWebFrame()) {
647         // FIXME: Remove at the soonest possible time.
648         webPage->send(Messages::WebPageProxy::SetRenderTreeSize(webPage->renderTreeSize()));
649         webPage->mainFrameDidLayout();
650     }
651 }
652
653 Frame* WebFrameLoaderClient::dispatchCreatePage(const NavigationAction& navigationAction)
654 {
655     WebPage* webPage = m_frame->page();
656     if (!webPage)
657         return nullptr;
658
659     // Just call through to the chrome client.
660     FrameLoadRequest request(m_frame->coreFrame()->document()->securityOrigin(), navigationAction.resourceRequest(), LockHistory::No, LockBackForwardList::No, MaybeSendReferrer, AllowNavigationToInvalidURL::Yes, NewFrameOpenerPolicy::Allow, navigationAction.shouldOpenExternalURLsPolicy());
661     Page* newPage = webPage->corePage()->chrome().createWindow(*m_frame->coreFrame(), request, WindowFeatures(), navigationAction);
662     if (!newPage)
663         return nullptr;
664     
665     return &newPage->mainFrame();
666 }
667
668 void WebFrameLoaderClient::dispatchShow()
669 {
670     WebPage* webPage = m_frame->page();
671     if (!webPage)
672         return;
673
674     webPage->show();
675 }
676
677 void WebFrameLoaderClient::dispatchDecidePolicyForResponse(const ResourceResponse& response, const ResourceRequest& request, FramePolicyFunction function)
678 {
679     WebPage* webPage = m_frame->page();
680     if (!webPage) {
681         function(PolicyIgnore);
682         return;
683     }
684
685     if (!request.url().string()) {
686         function(PolicyUse);
687         return;
688     }
689
690     RefPtr<API::Object> userData;
691
692     // Notify the bundle client.
693     WKBundlePagePolicyAction policy = webPage->injectedBundlePolicyClient().decidePolicyForResponse(webPage, m_frame, response, request, userData);
694     if (policy == WKBundlePagePolicyActionUse) {
695         function(PolicyUse);
696         return;
697     }
698
699     bool canShowMIMEType = webPage->canShowMIMEType(response.mimeType());
700
701     uint64_t listenerID = m_frame->setUpPolicyListener(WTFMove(function));
702     uint64_t policyAction;
703     DownloadID downloadID;
704
705     Ref<WebFrame> protect(*m_frame);
706     WebCore::Frame* coreFrame = m_frame->coreFrame();
707     if (!webPage->sendSync(Messages::WebPageProxy::DecidePolicyForResponseSync(m_frame->frameID(), SecurityOriginData::fromFrame(coreFrame), response, request, canShowMIMEType, listenerID, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())), Messages::WebPageProxy::DecidePolicyForResponseSync::Reply(policyAction, downloadID), Seconds::infinity(), IPC::SendSyncOption::InformPlatformProcessWillSuspend)) {
708         m_frame->didReceivePolicyDecision(listenerID, PolicyIgnore, 0, { });
709         return;
710     }
711
712     // We call this synchronously because CFNetwork can only convert a loading connection to a download from its didReceiveResponse callback.
713     m_frame->didReceivePolicyDecision(listenerID, static_cast<PolicyAction>(policyAction), 0, downloadID);
714 }
715
716 void WebFrameLoaderClient::dispatchDecidePolicyForNewWindowAction(const NavigationAction& navigationAction, const ResourceRequest& request, FormState* formState, const String& frameName, FramePolicyFunction function)
717 {
718     WebPage* webPage = m_frame->page();
719     if (!webPage) {
720         function(PolicyIgnore);
721         return;
722     }
723
724     RefPtr<API::Object> userData;
725
726     RefPtr<InjectedBundleNavigationAction> action = InjectedBundleNavigationAction::create(m_frame, navigationAction, formState);
727
728     // Notify the bundle client.
729     WKBundlePagePolicyAction policy = webPage->injectedBundlePolicyClient().decidePolicyForNewWindowAction(webPage, m_frame, action.get(), request, frameName, userData);
730     if (policy == WKBundlePagePolicyActionUse) {
731         function(PolicyUse);
732         return;
733     }
734
735
736     uint64_t listenerID = m_frame->setUpPolicyListener(WTFMove(function));
737
738     NavigationActionData navigationActionData;
739     navigationActionData.navigationType = action->navigationType();
740     navigationActionData.modifiers = action->modifiers();
741     navigationActionData.mouseButton = action->mouseButton();
742     navigationActionData.syntheticClickType = action->syntheticClickType();
743     navigationActionData.userGestureTokenIdentifier = WebProcess::singleton().userGestureTokenIdentifier(navigationAction.userGestureToken());
744     navigationActionData.canHandleRequest = webPage->canHandleRequest(request);
745     navigationActionData.shouldOpenExternalURLsPolicy = navigationAction.shouldOpenExternalURLsPolicy();
746     navigationActionData.downloadAttribute = navigationAction.downloadAttribute();
747
748     WebCore::Frame* coreFrame = m_frame ? m_frame->coreFrame() : nullptr;
749     webPage->send(Messages::WebPageProxy::DecidePolicyForNewWindowAction(m_frame->frameID(), SecurityOriginData::fromFrame(coreFrame), navigationActionData, request, frameName, listenerID, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
750 }
751
752 void WebFrameLoaderClient::dispatchDecidePolicyForNavigationAction(const NavigationAction& navigationAction, const ResourceRequest& request, FormState* formState, FramePolicyFunction function)
753 {
754     WebPage* webPage = m_frame->page();
755     if (!webPage) {
756         function(PolicyIgnore);
757         return;
758     }
759
760     // Always ignore requests with empty URLs. 
761     if (request.isEmpty()) {
762         function(PolicyIgnore);
763         return;
764     }
765
766     RefPtr<API::Object> userData;
767
768     RefPtr<InjectedBundleNavigationAction> action = InjectedBundleNavigationAction::create(m_frame, navigationAction, formState);
769
770     // Notify the bundle client.
771     WKBundlePagePolicyAction policy = webPage->injectedBundlePolicyClient().decidePolicyForNavigationAction(webPage, m_frame, action.get(), request, userData);
772     if (policy == WKBundlePagePolicyActionUse) {
773         function(PolicyUse);
774         return;
775     }
776     
777     uint64_t listenerID = m_frame->setUpPolicyListener(WTFMove(function));
778     uint64_t newNavigationID;
779     uint64_t policyAction;
780     DownloadID downloadID;
781
782     RefPtr<WebFrame> originatingFrame;
783     switch (action->navigationType()) {
784     case NavigationType::LinkClicked:
785         if (EventTarget* target = navigationAction.event()->target()) {
786             if (Node* node = target->toNode()) {
787                 if (Frame* frame = node->document().frame())
788                     originatingFrame = WebFrame::fromCoreFrame(*frame);
789             }
790         }
791         break;
792     case NavigationType::FormSubmitted:
793     case NavigationType::FormResubmitted:
794         if (formState)
795             originatingFrame = WebFrame::fromCoreFrame(*formState->sourceDocument().frame());
796         break;
797     case NavigationType::BackForward:
798     case NavigationType::Reload:
799     case NavigationType::Other:
800         break;
801     }
802
803     NavigationActionData navigationActionData;
804     navigationActionData.navigationType = action->navigationType();
805     navigationActionData.modifiers = action->modifiers();
806     navigationActionData.mouseButton = action->mouseButton();
807     navigationActionData.syntheticClickType = action->syntheticClickType();
808     navigationActionData.userGestureTokenIdentifier = WebProcess::singleton().userGestureTokenIdentifier(navigationAction.userGestureToken());
809     navigationActionData.canHandleRequest = webPage->canHandleRequest(request);
810     navigationActionData.shouldOpenExternalURLsPolicy = navigationAction.shouldOpenExternalURLsPolicy();
811     navigationActionData.downloadAttribute = navigationAction.downloadAttribute();
812
813     WebCore::Frame* coreFrame = m_frame->coreFrame();
814     WebDocumentLoader* documentLoader = static_cast<WebDocumentLoader*>(coreFrame->loader().policyDocumentLoader());
815     if (!documentLoader)
816         documentLoader = static_cast<WebDocumentLoader*>(coreFrame->loader().documentLoader());
817
818     // Notify the UIProcess.
819     Ref<WebFrame> protect(*m_frame);
820     WebCore::Frame* originatingCoreFrame = originatingFrame ? originatingFrame->coreFrame() : nullptr;
821     WebsitePolicies websitePolicies;
822     if (!webPage->sendSync(Messages::WebPageProxy::DecidePolicyForNavigationAction(m_frame->frameID(), SecurityOriginData::fromFrame(coreFrame), documentLoader->navigationID(), navigationActionData, originatingFrame ? originatingFrame->frameID() : 0, SecurityOriginData::fromFrame(originatingCoreFrame), navigationAction.resourceRequest(), request, listenerID, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())), Messages::WebPageProxy::DecidePolicyForNavigationAction::Reply(newNavigationID, policyAction, downloadID, websitePolicies))) {
823         m_frame->didReceivePolicyDecision(listenerID, PolicyIgnore, 0, { });
824         return;
825     }
826
827     // Only setUserContentExtensionsEnabled if it hasn't already been disabled by reloading without content blockers.
828     if (documentLoader->userContentExtensionsEnabled())
829         documentLoader->setUserContentExtensionsEnabled(websitePolicies.contentBlockersEnabled);
830
831     OptionSet<AutoplayQuirk> quirks;
832     auto allowedQuirks = websitePolicies.allowedAutoplayQuirks;
833
834     if (allowedQuirks.contains(WebsiteAutoplayQuirk::InheritedUserGestures))
835         quirks |= AutoplayQuirk::InheritedUserGestures;
836
837     if (allowedQuirks.contains(WebsiteAutoplayQuirk::SynthesizedPauseEvents))
838         quirks |= AutoplayQuirk::SynthesizedPauseEvents;
839
840     documentLoader->setAllowedAutoplayQuirks(quirks);
841
842     switch (websitePolicies.autoplayPolicy) {
843     case WebsiteAutoplayPolicy::Default:
844         documentLoader->setAutoplayPolicy(AutoplayPolicy::Default);
845         break;
846     case WebsiteAutoplayPolicy::Allow:
847         documentLoader->setAutoplayPolicy(AutoplayPolicy::Allow);
848         break;
849     case WebsiteAutoplayPolicy::AllowWithoutSound:
850         documentLoader->setAutoplayPolicy(AutoplayPolicy::AllowWithoutSound);
851         break;
852     case WebsiteAutoplayPolicy::Deny:
853         documentLoader->setAutoplayPolicy(AutoplayPolicy::Deny);
854         break;
855     }
856
857     // We call this synchronously because WebCore cannot gracefully handle a frame load without a synchronous navigation policy reply.
858     m_frame->didReceivePolicyDecision(listenerID, static_cast<PolicyAction>(policyAction), newNavigationID, downloadID);
859 }
860
861 void WebFrameLoaderClient::cancelPolicyCheck()
862 {
863     m_frame->invalidatePolicyListener();
864 }
865
866 void WebFrameLoaderClient::dispatchUnableToImplementPolicy(const ResourceError& error)
867 {
868     WebPage* webPage = m_frame->page();
869     if (!webPage)
870         return;
871
872     RefPtr<API::Object> userData;
873
874     // Notify the bundle client.
875     webPage->injectedBundlePolicyClient().unableToImplementPolicy(webPage, m_frame, error, userData);
876
877     // Notify the UIProcess.
878     webPage->send(Messages::WebPageProxy::UnableToImplementPolicy(m_frame->frameID(), error, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
879 }
880
881 void WebFrameLoaderClient::dispatchWillSendSubmitEvent(Ref<FormState>&& formState)
882 {
883     auto* webPage = m_frame->page();
884     if (!webPage)
885         return;
886
887     auto& form = formState->form();
888
889     ASSERT(formState->sourceDocument().frame());
890     auto* sourceFrame = WebFrame::fromCoreFrame(*formState->sourceDocument().frame());
891     ASSERT(sourceFrame);
892
893     webPage->injectedBundleFormClient().willSendSubmitEvent(webPage, &form, m_frame, sourceFrame, formState->textFieldValues());
894 }
895
896 void WebFrameLoaderClient::dispatchWillSubmitForm(FormState& formState, FramePolicyFunction function)
897 {
898     WebPage* webPage = m_frame->page();
899     if (!webPage)
900         return;
901
902     auto& form = formState.form();
903
904     auto* sourceFrame = WebFrame::fromCoreFrame(*formState.sourceDocument().frame());
905     ASSERT(sourceFrame);
906
907     auto& values = formState.textFieldValues();
908
909     RefPtr<API::Object> userData;
910     webPage->injectedBundleFormClient().willSubmitForm(webPage, &form, m_frame, sourceFrame, values, userData);
911
912     uint64_t listenerID = m_frame->setUpPolicyListener(WTFMove(function));
913
914     webPage->send(Messages::WebPageProxy::WillSubmitForm(m_frame->frameID(), sourceFrame->frameID(), values, listenerID, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
915 }
916
917 void WebFrameLoaderClient::revertToProvisionalState(DocumentLoader*)
918 {
919     notImplemented();
920 }
921
922 void WebFrameLoaderClient::setMainDocumentError(DocumentLoader*, const ResourceError& error)
923 {
924     if (!m_pluginView)
925         return;
926     
927     m_pluginView->manualLoadDidFail(error);
928     m_pluginView = nullptr;
929     m_hasSentResponseToPluginView = false;
930 }
931
932 void WebFrameLoaderClient::setMainFrameDocumentReady(bool)
933 {
934     notImplemented();
935 }
936
937 void WebFrameLoaderClient::startDownload(const ResourceRequest& request, const String& suggestedName)
938 {
939     m_frame->startDownload(request, suggestedName);
940 }
941
942 void WebFrameLoaderClient::willChangeTitle(DocumentLoader*)
943 {
944     notImplemented();
945 }
946
947 void WebFrameLoaderClient::didChangeTitle(DocumentLoader*)
948 {
949     notImplemented();
950 }
951
952 void WebFrameLoaderClient::willReplaceMultipartContent()
953 {
954     WebPage* webPage = m_frame->page();
955     if (!webPage)
956         return;
957     webPage->willReplaceMultipartContent(*m_frame);
958 }
959
960 void WebFrameLoaderClient::didReplaceMultipartContent()
961 {
962     WebPage* webPage = m_frame->page();
963     if (!webPage)
964         return;
965     webPage->didReplaceMultipartContent(*m_frame);
966 }
967
968 void WebFrameLoaderClient::committedLoad(DocumentLoader* loader, const char* data, int length)
969 {
970     if (!m_pluginView)
971         loader->commitData(data, length);
972
973     // If the document is a stand-alone media document, now is the right time to cancel the WebKit load.
974     // FIXME: This code should be shared across all ports. <http://webkit.org/b/48762>.
975     if (m_frame->coreFrame()->document()->isMediaDocument())
976         loader->cancelMainResourceLoad(pluginWillHandleLoadError(loader->response()));
977
978     // Calling commitData did not create the plug-in view.
979     if (!m_pluginView)
980         return;
981
982     if (!m_hasSentResponseToPluginView) {
983         m_pluginView->manualLoadDidReceiveResponse(loader->response());
984         // manualLoadDidReceiveResponse sets up a new stream to the plug-in. on a full-page plug-in, a failure in
985         // setting up this stream can cause the main document load to be cancelled, setting m_pluginView
986         // to null
987         if (!m_pluginView)
988             return;
989         m_hasSentResponseToPluginView = true;
990     }
991     m_pluginView->manualLoadDidReceiveData(data, length);
992 }
993
994 void WebFrameLoaderClient::finishedLoading(DocumentLoader* loader)
995 {
996     if (!m_pluginView) {
997         if (m_frameHasCustomContentProvider) {
998             WebPage* webPage = m_frame->page();
999             if (!webPage)
1000                 return;
1001
1002             RefPtr<SharedBuffer> mainResourceData = loader->mainResourceData();
1003             IPC::DataReference dataReference(reinterpret_cast<const uint8_t*>(mainResourceData ? mainResourceData->data() : 0), mainResourceData ? mainResourceData->size() : 0);
1004             webPage->send(Messages::WebPageProxy::DidFinishLoadingDataForCustomContentProvider(loader->response().suggestedFilename(), dataReference));
1005         }
1006
1007         return;
1008     }
1009
1010     // If we just received an empty response without any data, we won't have sent a response to the plug-in view.
1011     // Make sure to do this before calling manualLoadDidFinishLoading.
1012     if (!m_hasSentResponseToPluginView) {
1013         m_pluginView->manualLoadDidReceiveResponse(loader->response());
1014
1015         // Protect against the above call nulling out the plug-in (by trying to cancel the load for example).
1016         if (!m_pluginView)
1017             return;
1018     }
1019
1020     m_pluginView->manualLoadDidFinishLoading();
1021     m_pluginView = nullptr;
1022     m_hasSentResponseToPluginView = false;
1023 }
1024
1025 void WebFrameLoaderClient::updateGlobalHistory()
1026 {
1027     WebPage* webPage = m_frame->page();
1028     if (!webPage || !webPage->pageGroup()->isVisibleToHistoryClient())
1029         return;
1030
1031     DocumentLoader* loader = m_frame->coreFrame()->loader().documentLoader();
1032
1033     WebNavigationDataStore data;
1034     data.url = loader->url().string();
1035     // FIXME: Use direction of title.
1036     data.title = loader->title().string;
1037     data.originalRequest = loader->originalRequestCopy();
1038     data.response = loader->response();
1039
1040     webPage->send(Messages::WebPageProxy::DidNavigateWithNavigationData(data, m_frame->frameID()));
1041 }
1042
1043 void WebFrameLoaderClient::updateGlobalHistoryRedirectLinks()
1044 {
1045     WebPage* webPage = m_frame->page();
1046     if (!webPage || !webPage->pageGroup()->isVisibleToHistoryClient())
1047         return;
1048
1049     DocumentLoader* loader = m_frame->coreFrame()->loader().documentLoader();
1050     ASSERT(loader->unreachableURL().isEmpty());
1051
1052     // Client redirect
1053     if (!loader->clientRedirectSourceForHistory().isNull()) {
1054         webPage->send(Messages::WebPageProxy::DidPerformClientRedirect(
1055             loader->clientRedirectSourceForHistory(), loader->clientRedirectDestinationForHistory(), m_frame->frameID()));
1056     }
1057
1058     // Server redirect
1059     if (!loader->serverRedirectSourceForHistory().isNull()) {
1060         webPage->send(Messages::WebPageProxy::DidPerformServerRedirect(
1061             loader->serverRedirectSourceForHistory(), loader->serverRedirectDestinationForHistory(), m_frame->frameID()));
1062     }
1063 }
1064
1065 bool WebFrameLoaderClient::shouldGoToHistoryItem(HistoryItem* item) const
1066 {
1067     WebPage* webPage = m_frame->page();
1068     if (!webPage)
1069         return false;
1070     
1071     uint64_t itemID = WebBackForwardListProxy::idForItem(item);
1072     if (!itemID) {
1073         // We should never be considering navigating to an item that is not actually in the back/forward list.
1074         ASSERT_NOT_REACHED();
1075         return false;
1076     }
1077
1078     RefPtr<InjectedBundleBackForwardListItem> bundleItem = InjectedBundleBackForwardListItem::create(item);
1079     RefPtr<API::Object> userData;
1080
1081     // Ask the bundle client first
1082     bool shouldGoToBackForwardListItem = webPage->injectedBundleLoaderClient().shouldGoToBackForwardListItem(*webPage, *bundleItem, userData);
1083     if (!shouldGoToBackForwardListItem)
1084         return false;
1085
1086     webPage->send(Messages::WebPageProxy::WillGoToBackForwardListItem(itemID, UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
1087     return true;
1088 }
1089
1090 void WebFrameLoaderClient::didDisplayInsecureContent()
1091 {
1092     WebPage* webPage = m_frame->page();
1093     if (!webPage)
1094         return;
1095
1096     RefPtr<API::Object> userData;
1097
1098     webPage->injectedBundleLoaderClient().didDisplayInsecureContentForFrame(*webPage, *m_frame, userData);
1099
1100     webPage->send(Messages::WebPageProxy::DidDisplayInsecureContentForFrame(m_frame->frameID(), UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
1101 }
1102
1103 void WebFrameLoaderClient::didRunInsecureContent(SecurityOrigin&, const URL&)
1104 {
1105     WebPage* webPage = m_frame->page();
1106     if (!webPage)
1107         return;
1108
1109     RefPtr<API::Object> userData;
1110
1111     webPage->injectedBundleLoaderClient().didRunInsecureContentForFrame(*webPage, *m_frame, userData);
1112
1113     webPage->send(Messages::WebPageProxy::DidRunInsecureContentForFrame(m_frame->frameID(), UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
1114 }
1115
1116 void WebFrameLoaderClient::didDetectXSS(const URL&, bool)
1117 {
1118     WebPage* webPage = m_frame->page();
1119     if (!webPage)
1120         return;
1121
1122     RefPtr<API::Object> userData;
1123
1124     webPage->injectedBundleLoaderClient().didDetectXSSForFrame(*webPage, *m_frame, userData);
1125
1126     webPage->send(Messages::WebPageProxy::DidDetectXSSForFrame(m_frame->frameID(), UserData(WebProcess::singleton().transformObjectsToHandles(userData.get()).get())));
1127 }
1128
1129 ResourceError WebFrameLoaderClient::cancelledError(const ResourceRequest& request)
1130 {
1131     return WebKit::cancelledError(request);
1132 }
1133
1134 ResourceError WebFrameLoaderClient::blockedError(const ResourceRequest& request)
1135 {
1136     return WebKit::blockedError(request);
1137 }
1138
1139 ResourceError WebFrameLoaderClient::blockedByContentBlockerError(const ResourceRequest& request)
1140 {
1141     return WebKit::blockedByContentBlockerError(request);
1142 }
1143
1144 ResourceError WebFrameLoaderClient::cannotShowURLError(const ResourceRequest& request)
1145 {
1146     return WebKit::cannotShowURLError(request);
1147 }
1148
1149 ResourceError WebFrameLoaderClient::interruptedForPolicyChangeError(const ResourceRequest& request)
1150 {
1151     return WebKit::interruptedForPolicyChangeError(request);
1152 }
1153
1154 #if ENABLE(CONTENT_FILTERING)
1155 ResourceError WebFrameLoaderClient::blockedByContentFilterError(const ResourceRequest& request)
1156 {
1157     return WebKit::blockedByContentFilterError(request);
1158 }
1159 #endif
1160
1161 ResourceError WebFrameLoaderClient::cannotShowMIMETypeError(const ResourceResponse& response)
1162 {
1163     return WebKit::cannotShowMIMETypeError(response);
1164 }
1165
1166 ResourceError WebFrameLoaderClient::fileDoesNotExistError(const ResourceResponse& response)
1167 {
1168     return WebKit::fileDoesNotExistError(response);
1169 }
1170
1171 ResourceError WebFrameLoaderClient::pluginWillHandleLoadError(const ResourceResponse& response)
1172 {
1173     return WebKit::pluginWillHandleLoadError(response);
1174 }
1175
1176 bool WebFrameLoaderClient::shouldFallBack(const ResourceError& error)
1177 {
1178     static NeverDestroyed<const ResourceError> cancelledError(this->cancelledError(ResourceRequest()));
1179     static NeverDestroyed<const ResourceError> pluginWillHandleLoadError(this->pluginWillHandleLoadError(ResourceResponse()));
1180
1181     if (error.errorCode() == cancelledError.get().errorCode() && error.domain() == cancelledError.get().domain())
1182         return false;
1183
1184     if (error.errorCode() == pluginWillHandleLoadError.get().errorCode() && error.domain() == pluginWillHandleLoadError.get().domain())
1185         return false;
1186
1187     return true;
1188 }
1189
1190 bool WebFrameLoaderClient::canHandleRequest(const ResourceRequest&) const
1191 {
1192     notImplemented();
1193     return true;
1194 }
1195
1196 bool WebFrameLoaderClient::canShowMIMEType(const String& /*MIMEType*/) const
1197 {
1198     notImplemented();
1199     return true;
1200 }
1201
1202 bool WebFrameLoaderClient::canShowMIMETypeAsHTML(const String& /*MIMEType*/) const
1203 {
1204     return true;
1205 }
1206
1207 bool WebFrameLoaderClient::representationExistsForURLScheme(const String& /*URLScheme*/) const
1208 {
1209     notImplemented();
1210     return false;
1211 }
1212
1213 String WebFrameLoaderClient::generatedMIMETypeForURLScheme(const String& /*URLScheme*/) const
1214 {
1215     notImplemented();
1216     return String();
1217 }
1218
1219 void WebFrameLoaderClient::frameLoadCompleted()
1220 {
1221     // Note: Can be called multiple times.
1222     WebPage* webPage = m_frame->page();
1223     if (!webPage)
1224         return;
1225
1226     if (m_frame->isMainFrame() && !m_didCompletePageTransition) {
1227         webPage->didCompletePageTransition();
1228         m_didCompletePageTransition = true;
1229     }
1230 }
1231
1232 void WebFrameLoaderClient::saveViewStateToItem(HistoryItem& historyItem)
1233 {
1234 #if PLATFORM(IOS)
1235     if (m_frame->isMainFrame())
1236         m_frame->page()->savePageState(historyItem);
1237 #else
1238     UNUSED_PARAM(historyItem);
1239 #endif
1240 }
1241
1242 void WebFrameLoaderClient::restoreViewState()
1243 {
1244 #if PLATFORM(IOS)
1245     Frame& frame = *m_frame->coreFrame();
1246     HistoryItem* currentItem = frame.loader().history().currentItem();
1247     if (FrameView* view = frame.view()) {
1248         if (m_frame->isMainFrame())
1249             m_frame->page()->restorePageState(*currentItem);
1250         else if (!view->wasScrolledByUser())
1251             view->setScrollPosition(currentItem->scrollPosition());
1252     }
1253 #else
1254     // Inform the UI process of the scale factor.
1255     double scaleFactor = m_frame->coreFrame()->loader().history().currentItem()->pageScaleFactor();
1256
1257     // A scale factor of 0 means the history item has the default scale factor, thus we do not need to update it.
1258     if (scaleFactor)
1259         m_frame->page()->send(Messages::WebPageProxy::PageScaleFactorDidChange(scaleFactor));
1260
1261     // FIXME: This should not be necessary. WebCore should be correctly invalidating
1262     // the view on restores from the back/forward cache.
1263     if (m_frame->page() && m_frame == m_frame->page()->mainWebFrame())
1264         m_frame->page()->drawingArea()->setNeedsDisplay();
1265 #endif
1266 }
1267
1268 void WebFrameLoaderClient::provisionalLoadStarted()
1269 {
1270     WebPage* webPage = m_frame->page();
1271     if (!webPage)
1272         return;
1273
1274     if (m_frame->isMainFrame()) {
1275         webPage->didStartPageTransition();
1276         m_didCompletePageTransition = false;
1277     }
1278 }
1279
1280 void WebFrameLoaderClient::didFinishLoad()
1281 {
1282     // If we have a load listener, notify it.
1283     if (WebFrame::LoadListener* loadListener = m_frame->loadListener())
1284         loadListener->didFinishLoad(m_frame);
1285 }
1286
1287 void WebFrameLoaderClient::prepareForDataSourceReplacement()
1288 {
1289     notImplemented();
1290 }
1291
1292 Ref<DocumentLoader> WebFrameLoaderClient::createDocumentLoader(const ResourceRequest& request, const SubstituteData& substituteData)
1293 {
1294     return m_frame->page()->createDocumentLoader(*m_frame->coreFrame(), request, substituteData);
1295 }
1296
1297 void WebFrameLoaderClient::updateCachedDocumentLoader(WebCore::DocumentLoader& loader)
1298 {
1299     m_frame->page()->updateCachedDocumentLoader(static_cast<WebDocumentLoader&>(loader), *m_frame->coreFrame());
1300 }
1301
1302 void WebFrameLoaderClient::setTitle(const StringWithDirection& title, const URL& url)
1303 {
1304     WebPage* webPage = m_frame->page();
1305     if (!webPage || !webPage->pageGroup()->isVisibleToHistoryClient())
1306         return;
1307
1308     // FIXME: Use direction of title.
1309     webPage->send(Messages::WebPageProxy::DidUpdateHistoryTitle(title.string, url.string(), m_frame->frameID()));
1310 }
1311
1312 String WebFrameLoaderClient::userAgent(const URL& url)
1313 {
1314     WebPage* webPage = m_frame->page();
1315     if (!webPage)
1316         return String();
1317
1318     return webPage->userAgent(m_frame, url);
1319 }
1320
1321 String WebFrameLoaderClient::overrideContentSecurityPolicy() const
1322 {
1323     WebPage* webPage = m_frame->page();
1324     if (!webPage)
1325         return String();
1326
1327     return webPage->overrideContentSecurityPolicy();
1328 }
1329
1330 void WebFrameLoaderClient::savePlatformDataToCachedFrame(CachedFrame* cachedFrame)
1331 {
1332     WebPage* webPage = m_frame->page();
1333     if (!webPage)
1334         return;
1335
1336     HasInsecureContent hasInsecureContent;
1337     if (webPage->sendSync(Messages::WebPageProxy::HasInsecureContent(), Messages::WebPageProxy::HasInsecureContent::Reply(hasInsecureContent)))
1338         cachedFrame->setHasInsecureContent(hasInsecureContent);
1339 }
1340
1341 void WebFrameLoaderClient::transitionToCommittedFromCachedFrame(CachedFrame*)
1342 {
1343     const ResourceResponse& response = m_frame->coreFrame()->loader().documentLoader()->response();
1344     m_frameHasCustomContentProvider = m_frame->isMainFrame() && m_frame->page()->shouldUseCustomContentProviderForResponse(response);
1345     m_frameCameFromPageCache = true;
1346 }
1347
1348 void WebFrameLoaderClient::transitionToCommittedForNewPage()
1349 {
1350     WebPage* webPage = m_frame->page();
1351
1352     Color backgroundColor = webPage->drawsBackground() ? Color::white : Color::transparent;
1353     bool isMainFrame = m_frame->isMainFrame();
1354     bool isTransparent = !webPage->drawsBackground();
1355     bool shouldUseFixedLayout = isMainFrame && webPage->useFixedLayout();
1356     bool shouldDisableScrolling = isMainFrame && !webPage->mainFrameIsScrollable();
1357     bool shouldHideScrollbars = shouldDisableScrolling;
1358     IntRect fixedVisibleContentRect;
1359
1360 #if USE(COORDINATED_GRAPHICS)
1361     if (m_frame->coreFrame()->view())
1362         fixedVisibleContentRect = m_frame->coreFrame()->view()->fixedVisibleContentRect();
1363     if (shouldUseFixedLayout)
1364         shouldHideScrollbars = true;
1365 #endif
1366
1367     const ResourceResponse& response = m_frame->coreFrame()->loader().documentLoader()->response();
1368     m_frameHasCustomContentProvider = isMainFrame && webPage->shouldUseCustomContentProviderForResponse(response);
1369     m_frameCameFromPageCache = false;
1370
1371     ScrollbarMode defaultScrollbarMode = shouldHideScrollbars ? ScrollbarAlwaysOff : ScrollbarAuto;
1372
1373     m_frame->coreFrame()->createView(webPage->size(), backgroundColor, isTransparent,
1374         webPage->fixedLayoutSize(), fixedVisibleContentRect, shouldUseFixedLayout,
1375         defaultScrollbarMode, /* lock */ shouldHideScrollbars, defaultScrollbarMode, /* lock */ shouldHideScrollbars);
1376
1377     if (int minimumLayoutWidth = webPage->minimumLayoutSize().width()) {
1378         int minimumLayoutHeight = std::max(webPage->minimumLayoutSize().height(), 1);
1379         int maximumSize = std::numeric_limits<int>::max();
1380         m_frame->coreFrame()->view()->enableAutoSizeMode(true, IntSize(minimumLayoutWidth, minimumLayoutHeight), IntSize(maximumSize, maximumSize));
1381
1382         if (webPage->autoSizingShouldExpandToViewHeight())
1383             m_frame->coreFrame()->view()->setAutoSizeFixedMinimumHeight(webPage->size().height());
1384     }
1385
1386     if (auto viewportSizeForViewportUnits = webPage->viewportSizeForCSSViewportUnits())
1387         m_frame->coreFrame()->view()->setViewportSizeForCSSViewportUnits(*viewportSizeForViewportUnits);
1388     m_frame->coreFrame()->view()->setProhibitsScrolling(shouldDisableScrolling);
1389     m_frame->coreFrame()->view()->setVisualUpdatesAllowedByClient(!webPage->shouldExtendIncrementalRenderingSuppression());
1390 #if PLATFORM(COCOA)
1391     m_frame->coreFrame()->view()->setViewExposedRect(webPage->drawingArea()->viewExposedRect());
1392 #endif
1393 #if PLATFORM(IOS)
1394     m_frame->coreFrame()->view()->setDelegatesScrolling(true);
1395 #endif
1396
1397     if (webPage->scrollPinningBehavior() != DoNotPin)
1398         m_frame->coreFrame()->view()->setScrollPinningBehavior(webPage->scrollPinningBehavior());
1399
1400 #if USE(COORDINATED_GRAPHICS)
1401     if (shouldUseFixedLayout) {
1402         m_frame->coreFrame()->view()->setDelegatesScrolling(shouldUseFixedLayout);
1403         m_frame->coreFrame()->view()->setPaintsEntireContents(shouldUseFixedLayout);
1404         return;
1405     }
1406 #endif
1407 }
1408
1409 void WebFrameLoaderClient::didSaveToPageCache()
1410 {
1411     WebPage* webPage = m_frame->page();
1412     if (!webPage)
1413         return;
1414
1415     if (m_frame->isMainFrame())
1416         webPage->send(Messages::WebPageProxy::DidSaveToPageCache());
1417 }
1418
1419 void WebFrameLoaderClient::didRestoreFromPageCache()
1420 {
1421     m_frameCameFromPageCache = true;
1422 }
1423
1424 void WebFrameLoaderClient::dispatchDidBecomeFrameset(bool value)
1425 {
1426     WebPage* webPage = m_frame->page();
1427     if (!webPage)
1428         return;
1429
1430     webPage->send(Messages::WebPageProxy::FrameDidBecomeFrameSet(m_frame->frameID(), value));
1431 }
1432
1433 bool WebFrameLoaderClient::canCachePage() const
1434 {
1435     // We cannot cache frames that have custom representations because they are
1436     // rendered in the UIProcess.
1437     return !m_frameHasCustomContentProvider;
1438 }
1439
1440 void WebFrameLoaderClient::convertMainResourceLoadToDownload(DocumentLoader *documentLoader, SessionID sessionID, const ResourceRequest& request, const ResourceResponse& response)
1441 {
1442     m_frame->convertMainResourceLoadToDownload(documentLoader, sessionID, request, response);
1443 }
1444
1445 RefPtr<Frame> WebFrameLoaderClient::createFrame(const URL& url, const String& name, HTMLFrameOwnerElement& ownerElement,
1446     const String& referrer, bool /*allowsScrolling*/, int /*marginWidth*/, int /*marginHeight*/)
1447 {
1448     auto* webPage = m_frame->page();
1449
1450     auto subframe = WebFrame::createSubframe(webPage, name, &ownerElement);
1451     auto* coreSubframe = subframe->coreFrame();
1452     if (!coreSubframe)
1453         return nullptr;
1454
1455     // The creation of the frame may have run arbitrary JavaScript that removed it from the page already.
1456     if (!coreSubframe->page())
1457         return nullptr;
1458
1459     m_frame->coreFrame()->loader().loadURLIntoChildFrame(url, referrer, coreSubframe);
1460
1461     // The frame's onload handler may have removed it from the document.
1462     if (!subframe->coreFrame())
1463         return nullptr;
1464     ASSERT(subframe->coreFrame() == coreSubframe);
1465     if (!coreSubframe->tree().parent())
1466         return nullptr;
1467
1468     return coreSubframe;
1469 }
1470
1471 RefPtr<Widget> WebFrameLoaderClient::createPlugin(const IntSize&, HTMLPlugInElement& pluginElement, const URL& url, const Vector<String>& paramNames, const Vector<String>& paramValues, const String& mimeType, bool loadManually)
1472 {
1473     ASSERT(paramNames.size() == paramValues.size());
1474     ASSERT(m_frame->page());
1475
1476     Plugin::Parameters parameters;
1477     parameters.url = url;
1478     parameters.names = paramNames;
1479     parameters.values = paramValues;
1480     parameters.mimeType = mimeType;
1481     parameters.isFullFramePlugin = loadManually;
1482     parameters.shouldUseManualLoader = parameters.isFullFramePlugin && !m_frameCameFromPageCache;
1483 #if PLATFORM(COCOA)
1484     parameters.layerHostingMode = m_frame->page()->layerHostingMode();
1485 #endif
1486
1487 #if ENABLE(NETSCAPE_PLUGIN_API)
1488     auto plugin = m_frame->page()->createPlugin(m_frame, &pluginElement, parameters, parameters.mimeType);
1489     if (!plugin)
1490         return nullptr;
1491
1492     return PluginView::create(pluginElement, plugin.releaseNonNull(), parameters);
1493 #else
1494     UNUSED_PARAM(pluginElement);
1495     return nullptr;
1496 #endif
1497 }
1498
1499 void WebFrameLoaderClient::recreatePlugin(Widget* widget)
1500 {
1501 #if ENABLE(NETSCAPE_PLUGIN_API)
1502     ASSERT(widget);
1503     ASSERT(widget->isPluginViewBase());
1504     ASSERT(m_frame->page());
1505
1506     auto& pluginView = static_cast<PluginView&>(*widget);
1507     String newMIMEType;
1508     auto plugin = m_frame->page()->createPlugin(m_frame, pluginView.pluginElement(), pluginView.initialParameters(), newMIMEType);
1509     pluginView.recreateAndInitialize(plugin.releaseNonNull());
1510 #else
1511     UNUSED_PARAM(widget);
1512 #endif
1513 }
1514
1515 void WebFrameLoaderClient::redirectDataToPlugin(Widget& pluginWidget)
1516 {
1517     m_pluginView = static_cast<PluginView*>(&pluginWidget);
1518 }
1519
1520 #if ENABLE(WEBGL)
1521
1522 WebCore::WebGLLoadPolicy WebFrameLoaderClient::webGLPolicyForURL(const String& url) const
1523 {
1524     if (auto* webPage = m_frame->page())
1525         return webPage->webGLPolicyForURL(m_frame, url);
1526
1527     return WebGLAllowCreation;
1528 }
1529
1530 WebCore::WebGLLoadPolicy WebFrameLoaderClient::resolveWebGLPolicyForURL(const String& url) const
1531 {
1532     if (auto* webPage = m_frame->page())
1533         return webPage->resolveWebGLPolicyForURL(m_frame, url);
1534
1535     return WebGLAllowCreation;
1536 }
1537
1538 #endif
1539
1540 RefPtr<Widget> WebFrameLoaderClient::createJavaAppletWidget(const IntSize& pluginSize, HTMLAppletElement& appletElement, const URL&, const Vector<String>& paramNames, const Vector<String>& paramValues)
1541 {
1542 #if ENABLE(NETSCAPE_PLUGIN_API)
1543     auto plugin = createPlugin(pluginSize, appletElement, URL(), paramNames, paramValues, appletElement.serviceType(), false);
1544     if (!plugin) {
1545         if (auto* webPage = m_frame->page()) {
1546             auto frameURLString = m_frame->coreFrame()->loader().documentLoader()->responseURL().string();
1547             auto pageURLString = webPage->corePage()->mainFrame().loader().documentLoader()->responseURL().string();
1548             webPage->send(Messages::WebPageProxy::DidFailToInitializePlugin(appletElement.serviceType(), frameURLString, pageURLString));
1549         }
1550     }
1551     return plugin;
1552 #else
1553     UNUSED_PARAM(pluginSize);
1554     UNUSED_PARAM(appletElement);
1555     UNUSED_PARAM(paramNames);
1556     UNUSED_PARAM(paramValues);
1557     return nullptr;
1558 #endif
1559 }
1560
1561 static bool pluginSupportsExtension(const PluginData& pluginData, const String& extension)
1562 {
1563     ASSERT(extension.convertToASCIILowercase() == extension);
1564     Vector<MimeClassInfo> mimes;
1565     Vector<size_t> mimePluginIndices;
1566     pluginData.getWebVisibleMimesAndPluginIndices(mimes, mimePluginIndices);
1567     for (auto& mimeClassInfo : mimes) {
1568         if (mimeClassInfo.extensions.contains(extension))
1569             return true;
1570     }
1571     return false;
1572 }
1573
1574 ObjectContentType WebFrameLoaderClient::objectContentType(const URL& url, const String& mimeTypeIn)
1575 {
1576     // FIXME: This should eventually be merged with WebCore::FrameLoader::defaultObjectContentType.
1577
1578     String mimeType = mimeTypeIn;
1579     if (mimeType.isEmpty()) {
1580         String path = url.path();
1581         auto dotPosition = path.reverseFind('.');
1582         if (dotPosition == notFound)
1583             return ObjectContentType::Frame;
1584         String extension = path.substring(dotPosition + 1).convertToASCIILowercase();
1585
1586         // Try to guess the MIME type from the extension.
1587         mimeType = MIMETypeRegistry::getMIMETypeForExtension(extension);
1588         if (mimeType.isEmpty()) {
1589             // Check if there's a plug-in around that can handle the extension.
1590             if (WebPage* webPage = m_frame->page()) {
1591                 if (pluginSupportsExtension(webPage->corePage()->pluginData(), extension))
1592                     return ObjectContentType::PlugIn;
1593             }
1594             return ObjectContentType::Frame;
1595         }
1596     }
1597
1598     if (MIMETypeRegistry::isSupportedImageMIMEType(mimeType))
1599         return ObjectContentType::Image;
1600
1601     if (WebPage* webPage = m_frame->page()) {
1602         auto allowedPluginTypes = webFrame()->coreFrame()->loader().subframeLoader().allowPlugins()
1603             ? PluginData::AllPlugins : PluginData::OnlyApplicationPlugins;
1604         if (webPage->corePage()->pluginData().supportsMimeType(mimeType, allowedPluginTypes))
1605             return ObjectContentType::PlugIn;
1606     }
1607
1608     if (MIMETypeRegistry::isSupportedNonImageMIMEType(mimeType))
1609         return ObjectContentType::Frame;
1610
1611 #if PLATFORM(IOS)
1612     // iOS can render PDF in <object>/<embed> via PDFDocumentImage.
1613     if (MIMETypeRegistry::isPDFOrPostScriptMIMEType(mimeType))
1614         return ObjectContentType::Image;
1615 #endif
1616
1617     return ObjectContentType::None;
1618 }
1619
1620 String WebFrameLoaderClient::overrideMediaType() const
1621 {
1622     notImplemented();
1623     return String();
1624 }
1625
1626 void WebFrameLoaderClient::dispatchDidClearWindowObjectInWorld(DOMWrapperWorld& world)
1627 {
1628     WebPage* webPage = m_frame->page();
1629     if (!webPage)
1630         return;
1631
1632     webPage->injectedBundleLoaderClient().didClearWindowObjectForFrame(*webPage, *m_frame, world);
1633
1634
1635     WebAutomationSessionProxy* automationSessionProxy = WebProcess::singleton().automationSessionProxy();
1636     if (automationSessionProxy && world.isNormal())
1637         automationSessionProxy->didClearWindowObjectForFrame(*m_frame);
1638
1639 #if HAVE(ACCESSIBILITY) && PLATFORM(GTK)
1640     // Ensure the accessibility hierarchy is updated.
1641     webPage->updateAccessibilityTree();
1642 #endif
1643 }
1644
1645
1646 void WebFrameLoaderClient::dispatchGlobalObjectAvailable(DOMWrapperWorld& world)
1647 {
1648     WebPage* webPage = m_frame->page();
1649     if (!webPage)
1650         return;
1651     
1652     webPage->injectedBundleLoaderClient().globalObjectIsAvailableForFrame(*webPage, *m_frame, world);
1653 }
1654
1655 void WebFrameLoaderClient::dispatchWillDisconnectDOMWindowExtensionFromGlobalObject(WebCore::DOMWindowExtension* extension)
1656 {
1657     WebPage* webPage = m_frame->page();
1658     if (!webPage)
1659         return;
1660         
1661     webPage->injectedBundleLoaderClient().willDisconnectDOMWindowExtensionFromGlobalObject(*webPage, extension);
1662 }
1663
1664 void WebFrameLoaderClient::dispatchDidReconnectDOMWindowExtensionToGlobalObject(WebCore::DOMWindowExtension* extension)
1665 {
1666     WebPage* webPage = m_frame->page();
1667     if (!webPage)
1668         return;
1669         
1670     webPage->injectedBundleLoaderClient().didReconnectDOMWindowExtensionToGlobalObject(*webPage, extension);
1671 }
1672
1673 void WebFrameLoaderClient::dispatchWillDestroyGlobalObjectForDOMWindowExtension(WebCore::DOMWindowExtension* extension)
1674 {
1675     WebPage* webPage = m_frame->page();
1676     if (!webPage)
1677         return;
1678         
1679     webPage->injectedBundleLoaderClient().willDestroyGlobalObjectForDOMWindowExtension(*webPage, extension);
1680 }
1681
1682 void WebFrameLoaderClient::registerForIconNotification(bool /*listen*/)
1683 {
1684     notImplemented();
1685 }
1686
1687 #if PLATFORM(COCOA)
1688     
1689 RemoteAXObjectRef WebFrameLoaderClient::accessibilityRemoteObject() 
1690 {
1691     WebPage* webPage = m_frame->page();
1692     if (!webPage)
1693         return 0;
1694     
1695     return webPage->accessibilityRemoteObject();
1696 }
1697     
1698 NSCachedURLResponse *WebFrameLoaderClient::willCacheResponse(DocumentLoader*, unsigned long identifier, NSCachedURLResponse* response) const
1699 {
1700     WebPage* webPage = m_frame->page();
1701     if (!webPage)
1702         return response;
1703
1704     return webPage->injectedBundleResourceLoadClient().shouldCacheResponse(*webPage, *m_frame, identifier) ? response : nil;
1705 }
1706
1707 NSDictionary *WebFrameLoaderClient::dataDetectionContext()
1708 {
1709     WebPage* webPage = m_frame->page();
1710     if (!webPage)
1711         return nil;
1712
1713     return webPage->dataDetectionContext();
1714 }
1715
1716 #endif // PLATFORM(COCOA)
1717
1718 bool WebFrameLoaderClient::shouldAlwaysUsePluginDocument(const String& /*mimeType*/) const
1719 {
1720     notImplemented();
1721     return false;
1722 }
1723
1724 void WebFrameLoaderClient::didChangeScrollOffset()
1725 {
1726     WebPage* webPage = m_frame->page();
1727     if (!webPage)
1728         return;
1729
1730     webPage->didChangeScrollOffsetForFrame(m_frame->coreFrame());
1731 }
1732
1733 bool WebFrameLoaderClient::allowScript(bool enabledPerSettings)
1734 {
1735     if (!enabledPerSettings)
1736         return false;
1737
1738     Frame* coreFrame = m_frame->coreFrame();
1739
1740     if (coreFrame->document()->isPluginDocument()) {
1741         PluginDocument* pluginDocument = static_cast<PluginDocument*>(coreFrame->document());
1742
1743         if (pluginDocument->pluginWidget() && pluginDocument->pluginWidget()->isPluginView()) {
1744             PluginView* pluginView = static_cast<PluginView*>(pluginDocument->pluginWidget());
1745
1746             if (!pluginView->shouldAllowScripting())
1747                 return false;
1748         }
1749     }
1750
1751     return true;
1752 }
1753
1754 bool WebFrameLoaderClient::shouldForceUniversalAccessFromLocalURL(const WebCore::URL& url)
1755 {
1756     WebPage* webPage = m_frame->page();
1757     if (!webPage)
1758         return false;
1759
1760     return webPage->injectedBundleLoaderClient().shouldForceUniversalAccessFromLocalURL(*webPage, url);
1761 }
1762
1763 Ref<FrameNetworkingContext> WebFrameLoaderClient::createNetworkingContext()
1764 {
1765     return WebFrameNetworkingContext::create(m_frame);
1766 }
1767
1768 #if ENABLE(CONTENT_FILTERING)
1769
1770 void WebFrameLoaderClient::contentFilterDidBlockLoad(WebCore::ContentFilterUnblockHandler unblockHandler)
1771 {
1772     if (!unblockHandler.needsUIProcess()) {
1773         m_frame->coreFrame()->loader().policyChecker().setContentFilterUnblockHandler(WTFMove(unblockHandler));
1774         return;
1775     }
1776
1777     if (WebPage* webPage { m_frame->page() })
1778         webPage->send(Messages::WebPageProxy::ContentFilterDidBlockLoadForFrame(unblockHandler, m_frame->frameID()));
1779 }
1780
1781 #endif
1782
1783 #if ENABLE(REQUEST_AUTOCOMPLETE)
1784
1785 void WebFrameLoaderClient::didRequestAutocomplete(Ref<WebCore::FormState>&&)
1786 {
1787 }
1788
1789 #endif
1790
1791 void WebFrameLoaderClient::prefetchDNS(const String& hostname)
1792 {
1793     WebProcess::singleton().prefetchDNS(hostname);
1794 }
1795
1796 void WebFrameLoaderClient::didRestoreScrollPosition()
1797 {
1798     WebPage* webPage = m_frame->page();
1799     if (!webPage)
1800         return;
1801
1802     webPage->didRestoreScrollPosition();
1803 }
1804
1805 bool WebFrameLoaderClient::useIconLoadingClient()
1806 {
1807     return m_useIconLoadingClient;
1808 }
1809
1810 void WebFrameLoaderClient::getLoadDecisionForIcon(const LinkIcon& icon, uint64_t callbackID)
1811 {
1812     if (WebPage* webPage { m_frame->page() })
1813         webPage->send(Messages::WebPageProxy::GetLoadDecisionForIcon(icon, callbackID));
1814 }
1815
1816 void WebFrameLoaderClient::finishedLoadingIcon(uint64_t loadIdentifier, SharedBuffer* data)
1817 {
1818     if (WebPage* webPage { m_frame->page() }) {
1819         if (data)
1820             webPage->send(Messages::WebPageProxy::FinishedLoadingIcon(loadIdentifier, { reinterpret_cast<const uint8_t*>(data->data()), data->size() }));
1821         else
1822             webPage->send(Messages::WebPageProxy::FinishedLoadingIcon(loadIdentifier, { nullptr, 0 }));
1823     }
1824 }
1825
1826 } // namespace WebKit