2 * Copyright (C) 2007 Kevin Ollivier <kevino@theolliviers.com>
3 * Copyright (C) 2011 Apple Inc. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "FrameLoaderClientWx.h"
32 #include <JavaScriptCore/JavaScript.h>
33 #include <JavaScriptCore/APICast.h>
35 #include "DocumentLoader.h"
36 #include "FormState.h"
38 #include "FrameLoaderTypes.h"
39 #include "FrameView.h"
40 #include "FrameTree.h"
41 #include "PluginView.h"
42 #include "HTMLFormElement.h"
43 #include "HTMLFrameOwnerElement.h"
44 #include "NotImplemented.h"
46 #include "PlatformString.h"
47 #include "PluginView.h"
48 #include "ProgressTracker.h"
49 #include "RenderPart.h"
50 #include "ResourceError.h"
51 #include "ResourceResponse.h"
52 #include "ScriptController.h"
53 #include <wtf/PassRefPtr.h>
54 #include <wtf/RefPtr.h>
58 #include "FrameNetworkingContextWx.h"
60 #include "WebFramePrivate.h"
62 #include "WebViewPrivate.h"
66 inline int wxNavTypeFromWebNavType(NavigationType type){
67 if (type == NavigationTypeLinkClicked)
68 return wxWEBVIEW_NAV_LINK_CLICKED;
70 if (type == NavigationTypeFormSubmitted)
71 return wxWEBVIEW_NAV_FORM_SUBMITTED;
73 if (type == NavigationTypeBackForward)
74 return wxWEBVIEW_NAV_BACK_NEXT;
76 if (type == NavigationTypeReload)
77 return wxWEBVIEW_NAV_RELOAD;
79 if (type == NavigationTypeFormResubmitted)
80 return wxWEBVIEW_NAV_FORM_RESUBMITTED;
82 return wxWEBVIEW_NAV_OTHER;
85 FrameLoaderClientWx::FrameLoaderClientWx()
88 , m_hasSentResponseToPlugin(false)
94 FrameLoaderClientWx::~FrameLoaderClientWx()
98 void FrameLoaderClientWx::setFrame(wxWebFrame *frame)
101 m_frame = m_webFrame->m_impl->frame;
104 void FrameLoaderClientWx::setWebView(wxWebView *webview)
109 bool FrameLoaderClientWx::hasWebView() const
111 return m_webView != NULL;
114 bool FrameLoaderClientWx::hasBackForwardList() const
121 void FrameLoaderClientWx::resetBackForwardList()
127 bool FrameLoaderClientWx::provisionalItemIsTarget() const
133 void FrameLoaderClientWx::makeRepresentation(DocumentLoader*)
139 void FrameLoaderClientWx::forceLayout()
145 void FrameLoaderClientWx::forceLayoutForNonHTML()
151 void FrameLoaderClientWx::updateHistoryForCommit()
157 void FrameLoaderClientWx::updateHistoryForBackForwardNavigation()
163 void FrameLoaderClientWx::updateHistoryForReload()
169 void FrameLoaderClientWx::updateHistoryForStandardLoad()
175 void FrameLoaderClientWx::updateHistoryForInternalLoad()
181 void FrameLoaderClientWx::updateHistoryAfterClientRedirect()
187 void FrameLoaderClientWx::setCopiesOnScroll()
189 // apparently mac specific
194 LoadErrorResetToken* FrameLoaderClientWx::tokenForLoadErrorReset()
201 void FrameLoaderClientWx::resetAfterLoadError(LoadErrorResetToken*)
207 void FrameLoaderClientWx::doNotResetAfterLoadError(LoadErrorResetToken*)
213 void FrameLoaderClientWx::willCloseDocument()
219 void FrameLoaderClientWx::detachedFromParent2()
225 void FrameLoaderClientWx::detachedFromParent3()
230 void FrameLoaderClientWx::dispatchDidHandleOnloadEvents()
233 wxWebViewLoadEvent wkEvent(m_webView);
234 wkEvent.SetState(wxWEBVIEW_LOAD_ONLOAD_HANDLED);
235 wkEvent.SetURL(m_frame->loader()->documentLoader()->request().url().string());
236 wkEvent.SetFrame(m_webFrame);
237 m_webView->GetEventHandler()->ProcessEvent(wkEvent);
242 void FrameLoaderClientWx::dispatchDidReceiveServerRedirectForProvisionalLoad()
248 void FrameLoaderClientWx::dispatchDidCancelClientRedirect()
254 void FrameLoaderClientWx::dispatchWillPerformClientRedirect(const KURL&,
262 void FrameLoaderClientWx::dispatchDidChangeLocationWithinPage()
267 void FrameLoaderClientWx::dispatchDidPushStateWithinPage()
272 void FrameLoaderClientWx::dispatchDidReplaceStateWithinPage()
277 void FrameLoaderClientWx::dispatchDidPopStateWithinPage()
282 void FrameLoaderClientWx::dispatchWillClose()
288 void FrameLoaderClientWx::dispatchDidStartProvisionalLoad()
291 wxWebViewLoadEvent wkEvent(m_webView);
292 wkEvent.SetState(wxWEBVIEW_LOAD_NEGOTIATING);
293 wkEvent.SetURL(m_frame->loader()->provisionalDocumentLoader()->request().url().string());
294 wkEvent.SetFrame(m_webFrame);
295 m_webView->GetEventHandler()->ProcessEvent(wkEvent);
300 void FrameLoaderClientWx::dispatchDidReceiveTitle(const StringWithDirection& title)
303 // FIXME: use direction of title.
304 if (m_webFrame == m_webView->GetMainFrame())
305 m_webView->SetPageTitle(title.string());
307 wxWebViewReceivedTitleEvent wkEvent(m_webView);
308 wkEvent.SetFrame(m_webFrame);
309 wkEvent.SetTitle(title.string());
310 m_webView->GetEventHandler()->ProcessEvent(wkEvent);
315 void FrameLoaderClientWx::dispatchDidCommitLoad()
318 wxWebViewLoadEvent wkEvent(m_webView);
319 wkEvent.SetState(wxWEBVIEW_LOAD_TRANSFERRING);
320 wkEvent.SetURL(m_frame->loader()->documentLoader()->request().url().string());
321 wkEvent.SetFrame(m_webFrame);
322 m_webView->GetEventHandler()->ProcessEvent(wkEvent);
326 void FrameLoaderClientWx::dispatchDidFinishDocumentLoad()
329 wxWebViewLoadEvent wkEvent(m_webView);
330 wkEvent.SetState(wxWEBVIEW_LOAD_DOC_COMPLETED);
331 wkEvent.SetURL(m_frame->document()->url().string());
332 wkEvent.SetFrame(m_webFrame);
333 m_webView->GetEventHandler()->ProcessEvent(wkEvent);
337 void FrameLoaderClientWx::dispatchDidChangeIcons(WebCore::IconType)
342 void FrameLoaderClientWx::dispatchDidFinishLoad()
348 void FrameLoaderClientWx::dispatchDidFirstLayout()
353 void FrameLoaderClientWx::dispatchDidFirstVisuallyNonEmptyLayout()
358 void FrameLoaderClientWx::dispatchShow()
364 void FrameLoaderClientWx::cancelPolicyCheck()
370 void FrameLoaderClientWx::dispatchWillSubmitForm(FramePolicyFunction function,
371 PassRefPtr<FormState>)
373 // FIXME: Send an event to allow for alerts and cancellation
376 (m_frame->loader()->policyChecker()->*function)(PolicyUse);
380 void FrameLoaderClientWx::dispatchDidLoadMainResource(DocumentLoader*)
386 void FrameLoaderClientWx::revertToProvisionalState(DocumentLoader*)
391 void FrameLoaderClientWx::postProgressStartedNotification()
396 void FrameLoaderClientWx::postProgressEstimateChangedNotification()
401 void FrameLoaderClientWx::postProgressFinishedNotification()
404 wxWebViewLoadEvent wkEvent(m_webView);
405 wkEvent.SetState(wxWEBVIEW_LOAD_DL_COMPLETED);
406 wkEvent.SetURL(m_frame->document()->url().string());
407 wkEvent.SetFrame(m_webFrame);
408 m_webView->GetEventHandler()->ProcessEvent(wkEvent);
412 void FrameLoaderClientWx::progressStarted()
418 void FrameLoaderClientWx::progressCompleted()
424 void FrameLoaderClientWx::setMainFrameDocumentReady(bool b)
427 // this is only interesting once we provide an external API for the DOM
431 void FrameLoaderClientWx::willChangeTitle(DocumentLoader*)
437 void FrameLoaderClientWx::didChangeTitle(DocumentLoader *l)
439 setTitle(l->title(), l->url());
443 void FrameLoaderClientWx::finishedLoading(DocumentLoader* loader)
447 loader->writer()->setEncoding(m_response.textEncodingName(), false);
451 m_pluginView->didFinishLoading();
453 m_hasSentResponseToPlugin = false;
457 bool FrameLoaderClientWx::canShowMIMETypeAsHTML(const String& MIMEType) const
464 bool FrameLoaderClientWx::canShowMIMEType(const String& MIMEType) const
471 bool FrameLoaderClientWx::representationExistsForURLScheme(const String& URLScheme) const
478 String FrameLoaderClientWx::generatedMIMETypeForURLScheme(const String& URLScheme) const
485 void FrameLoaderClientWx::frameLoadCompleted()
490 void FrameLoaderClientWx::saveViewStateToItem(HistoryItem*)
495 void FrameLoaderClientWx::restoreViewState()
500 void FrameLoaderClientWx::restoreScrollPositionAndViewState()
506 void FrameLoaderClientWx::provisionalLoadStarted()
512 bool FrameLoaderClientWx::shouldTreatURLAsSameAsCurrent(const KURL&) const
519 void FrameLoaderClientWx::addHistoryItemForFragmentScroll()
525 void FrameLoaderClientWx::didFinishLoad()
531 void FrameLoaderClientWx::prepareForDataSourceReplacement()
537 void FrameLoaderClientWx::setTitle(const StringWithDirection& title, const KURL&)
543 String FrameLoaderClientWx::userAgent(const KURL&)
545 // FIXME: Use the new APIs introduced by the GTK port to fill in these values.
546 return String("Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/418.9.1 (KHTML, like Gecko) Safari/419.3");
549 void FrameLoaderClientWx::dispatchDidReceiveIcon()
554 void FrameLoaderClientWx::frameLoaderDestroyed()
563 bool FrameLoaderClientWx::canHandleRequest(const WebCore::ResourceRequest&) const
569 void FrameLoaderClientWx::partClearedInBegin()
574 void FrameLoaderClientWx::updateGlobalHistory()
579 void FrameLoaderClientWx::updateGlobalHistoryRedirectLinks()
584 bool FrameLoaderClientWx::shouldGoToHistoryItem(WebCore::HistoryItem*) const
590 bool FrameLoaderClientWx::shouldStopLoadingForHistoryItem(WebCore::HistoryItem*) const
595 void FrameLoaderClientWx::didDisplayInsecureContent()
600 void FrameLoaderClientWx::didRunInsecureContent(WebCore::SecurityOrigin*, const KURL&)
605 void FrameLoaderClientWx::didDetectXSS(const KURL&, bool)
610 void FrameLoaderClientWx::saveScrollPositionAndViewStateToItem(WebCore::HistoryItem*)
615 bool FrameLoaderClientWx::canCachePage() const
620 void FrameLoaderClientWx::setMainDocumentError(WebCore::DocumentLoader* loader, const WebCore::ResourceError&)
623 loader->writer()->setEncoding(m_response.textEncodingName(), false);
628 // FIXME: This function should be moved into WebCore.
629 void FrameLoaderClientWx::committedLoad(WebCore::DocumentLoader* loader, const char* data, int length)
634 loader->commitData(data, length);
636 // We re-check here as the plugin can have been created
638 if (!m_hasSentResponseToPlugin) {
639 m_pluginView->didReceiveResponse(loader->response());
640 // didReceiveResponse sets up a new stream to the plug-in. on a full-page plug-in, a failure in
641 // setting up this stream can cause the main document load to be cancelled, setting m_pluginView
645 m_hasSentResponseToPlugin = true;
647 m_pluginView->didReceiveData(data, length);
651 WebCore::ResourceError FrameLoaderClientWx::cancelledError(const WebCore::ResourceRequest& request)
654 return ResourceError(String(), WebKitErrorCannotShowURL, request.url().string(), String());
657 WebCore::ResourceError FrameLoaderClientWx::blockedError(const ResourceRequest& request)
660 return ResourceError(String(), WebKitErrorCannotShowURL, request.url().string(), String());
663 WebCore::ResourceError FrameLoaderClientWx::cannotShowURLError(const WebCore::ResourceRequest& request)
665 return ResourceError(String(), WebKitErrorCannotShowURL, request.url().string(), String());
668 WebCore::ResourceError FrameLoaderClientWx::interruptedForPolicyChangeError(const WebCore::ResourceRequest& request)
671 return ResourceError(String(), WebKitErrorFrameLoadInterruptedByPolicyChange, request.url().string(), String());
674 WebCore::ResourceError FrameLoaderClientWx::cannotShowMIMETypeError(const WebCore::ResourceResponse& response)
677 return ResourceError(String(), WebKitErrorCannotShowMIMEType, response.url().string(), String());
680 WebCore::ResourceError FrameLoaderClientWx::fileDoesNotExistError(const WebCore::ResourceResponse& response)
683 return ResourceError(String(), WebKitErrorCannotShowURL, response.url().string(), String());
686 bool FrameLoaderClientWx::shouldFallBack(const WebCore::ResourceError& error)
692 WTF::PassRefPtr<DocumentLoader> FrameLoaderClientWx::createDocumentLoader(const ResourceRequest& request, const SubstituteData& substituteData)
694 return DocumentLoader::create(request, substituteData);
697 void FrameLoaderClientWx::download(ResourceHandle*, const ResourceRequest&, const ResourceRequest&, const ResourceResponse&)
702 void FrameLoaderClientWx::assignIdentifierToInitialRequest(unsigned long identifier, DocumentLoader*, const ResourceRequest&)
707 void FrameLoaderClientWx::dispatchWillSendRequest(DocumentLoader*, unsigned long, ResourceRequest& request, const ResourceResponse& response)
712 bool FrameLoaderClientWx::shouldUseCredentialStorage(DocumentLoader*, unsigned long)
718 void FrameLoaderClientWx::dispatchDidReceiveAuthenticationChallenge(DocumentLoader*, unsigned long, const AuthenticationChallenge&)
723 void FrameLoaderClientWx::dispatchDidCancelAuthenticationChallenge(DocumentLoader*, unsigned long, const AuthenticationChallenge&)
728 void FrameLoaderClientWx::dispatchDidReceiveResponse(DocumentLoader* loader, unsigned long id, const ResourceResponse& response)
731 m_response = response;
735 void FrameLoaderClientWx::dispatchDidReceiveContentLength(DocumentLoader* loader, unsigned long id, int length)
740 void FrameLoaderClientWx::dispatchDidFinishLoading(DocumentLoader*, unsigned long)
745 void FrameLoaderClientWx::dispatchDidFailLoading(DocumentLoader* loader, unsigned long, const ResourceError&)
748 loader->writer()->setEncoding(m_response.textEncodingName(), false);
752 wxWebViewLoadEvent wkEvent(m_webView);
753 wkEvent.SetState(wxWEBVIEW_LOAD_FAILED);
754 wkEvent.SetURL(m_frame->loader()->documentLoader()->request().url().string());
755 wkEvent.SetFrame(m_webFrame);
756 m_webView->GetEventHandler()->ProcessEvent(wkEvent);
760 bool FrameLoaderClientWx::dispatchDidLoadResourceFromMemoryCache(DocumentLoader*, const ResourceRequest&, const ResourceResponse&, int)
766 void FrameLoaderClientWx::dispatchDidFailProvisionalLoad(const ResourceError&)
771 void FrameLoaderClientWx::dispatchDidFailLoad(const ResourceError&)
776 Frame* FrameLoaderClientWx::dispatchCreatePage(const NavigationAction&)
782 void FrameLoaderClientWx::dispatchDecidePolicyForResponse(FramePolicyFunction function, const ResourceResponse& response, const ResourceRequest& request)
788 (m_frame->loader()->policyChecker()->*function)(PolicyUse);
791 void FrameLoaderClientWx::dispatchDecidePolicyForNewWindowAction(FramePolicyFunction function, const NavigationAction&, const ResourceRequest& request, PassRefPtr<FormState>, const String& targetName)
797 wxWebViewNewWindowEvent wkEvent(m_webView);
798 wkEvent.SetURL(request.url().string());
799 wkEvent.SetTargetName(targetName);
800 wkEvent.SetFrame(m_webFrame);
801 if (m_webView->GetEventHandler()->ProcessEvent(wkEvent)) {
802 // if the app handles and doesn't skip the event,
803 // from WebKit's perspective treat it as blocked / ignored
804 (m_frame->loader()->policyChecker()->*function)(PolicyIgnore);
809 (m_frame->loader()->policyChecker()->*function)(PolicyUse);
812 void FrameLoaderClientWx::dispatchDecidePolicyForNavigationAction(FramePolicyFunction function, const NavigationAction& action, const ResourceRequest& request, PassRefPtr<FormState>)
818 wxWebViewBeforeLoadEvent wkEvent(m_webView);
819 wkEvent.SetNavigationType(wxNavTypeFromWebNavType(action.type()));
820 wkEvent.SetURL(request.url().string());
821 wkEvent.SetFrame(m_webFrame);
822 m_webView->GetEventHandler()->ProcessEvent(wkEvent);
823 if (wkEvent.IsCancelled())
824 (m_frame->loader()->policyChecker()->*function)(PolicyIgnore);
826 (m_frame->loader()->policyChecker()->*function)(PolicyUse);
831 void FrameLoaderClientWx::dispatchUnableToImplementPolicy(const ResourceError&)
836 void FrameLoaderClientWx::startDownload(const ResourceRequest&, const String& /* suggestedName */)
841 PassRefPtr<Frame> FrameLoaderClientWx::createFrame(const KURL& url, const String& name, HTMLFrameOwnerElement* ownerElement,
842 const String& referrer, bool allowsScrolling, int marginWidth, int marginHeight)
844 WebViewFrameData* data = new WebViewFrameData();
846 data->ownerElement = ownerElement;
848 data->referrer = referrer;
849 data->allowsScrolling = allowsScrolling;
850 data->marginWidth = marginWidth;
851 data->marginHeight = marginHeight;
853 wxWebFrame* newFrame = new wxWebFrame(m_webView, m_webFrame, data);
855 RefPtr<Frame> childFrame = adoptRef(newFrame->m_impl->frame);
857 // The creation of the frame may have run arbitrary JavaScript that removed it from the page already.
858 if (!childFrame->page())
861 m_frame->loader()->loadURLIntoChildFrame(url, referrer, childFrame.get());
863 // The frame's onload handler may have removed it from the document.
864 if (!childFrame->tree()->parent())
867 return childFrame.release();
870 void FrameLoaderClientWx::didTransferChildFrameToNewDocument(Page*)
874 void FrameLoaderClientWx::transferLoadingResourceFromPage(ResourceLoader*, const ResourceRequest&, Page*)
878 ObjectContentType FrameLoaderClientWx::objectContentType(const KURL& url, const String& mimeType, bool shouldPreferPlugInsForImages)
881 return ObjectContentType();
884 PassRefPtr<Widget> FrameLoaderClientWx::createPlugin(const IntSize& size, HTMLPlugInElement* element, const KURL& url, const Vector<String>& paramNames, const Vector<String>& paramValues, const String& mimeType, bool loadManually)
886 #if __WXMSW__ || __WXMAC__
887 RefPtr<PluginView> pv = PluginView::create(m_frame, size, element, url, paramNames, paramValues, mimeType, loadManually);
888 if (pv->status() == PluginStatusLoadedSuccessfully)
894 void FrameLoaderClientWx::redirectDataToPlugin(Widget* pluginWidget)
896 ASSERT(!m_pluginView);
897 m_pluginView = static_cast<PluginView*>(pluginWidget);
898 m_hasSentResponseToPlugin = false;
901 ResourceError FrameLoaderClientWx::pluginWillHandleLoadError(const ResourceResponse& response)
904 return ResourceError(String(), WebKitErrorCannotLoadPlugIn, response.url().string(), String());
907 PassRefPtr<Widget> FrameLoaderClientWx::createJavaAppletWidget(const IntSize&, HTMLAppletElement*, const KURL& baseURL,
908 const Vector<String>& paramNames, const Vector<String>& paramValues)
914 String FrameLoaderClientWx::overrideMediaType() const
920 void FrameLoaderClientWx::dispatchDidClearWindowObjectInWorld(DOMWrapperWorld* world)
922 if (world != mainThreadNormalWorld())
926 wxWebViewWindowObjectClearedEvent wkEvent(m_webView);
927 Frame* coreFrame = m_webView->GetMainFrame()->GetFrame();
928 JSGlobalContextRef context = toGlobalRef(coreFrame->script()->globalObject(mainThreadNormalWorld())->globalExec());
929 JSObjectRef windowObject = toRef(coreFrame->script()->globalObject(mainThreadNormalWorld()));
930 wkEvent.SetJSContext(context);
931 wkEvent.SetWindowObject(windowObject);
932 m_webView->GetEventHandler()->ProcessEvent(wkEvent);
936 void FrameLoaderClientWx::documentElementAvailable()
940 void FrameLoaderClientWx::didPerformFirstNavigation() const
945 void FrameLoaderClientWx::registerForIconNotification(bool listen)
950 void FrameLoaderClientWx::savePlatformDataToCachedFrame(CachedFrame*)
955 void FrameLoaderClientWx::transitionToCommittedFromCachedFrame(CachedFrame*)
960 void FrameLoaderClientWx::transitionToCommittedForNewPage()
966 IntSize size = IntRect(m_webView->GetRect()).size();
967 // FIXME: This value should be gotten from m_webView->IsTransparent();
968 // but transitionToCommittedForNewPage() can be called while m_webView is
969 // still being initialized.
970 bool transparent = false;
971 Color backgroundColor = transparent ? WebCore::Color::transparent : WebCore::Color::white;
974 m_frame->createView(size, backgroundColor, transparent, IntSize(), false);
977 void FrameLoaderClientWx::didSaveToPageCache()
981 void FrameLoaderClientWx::didRestoreFromPageCache()
985 void FrameLoaderClientWx::dispatchDidBecomeFrameset(bool)
989 bool FrameLoaderClientWx::shouldUsePluginDocument(const String &mimeType) const
991 // NOTE: Plugin Documents are used for viewing PDFs, etc. inline, and should
992 // not be used for pages with plugins in them.
996 PassRefPtr<FrameNetworkingContext> FrameLoaderClientWx::createNetworkingContext()
998 return FrameNetworkingContextWx::create(m_frame);