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