1/*
2 * Copyright (C) 2010, 2011 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 "WebFrameProxy.h"
28
29#include "WebCertificateInfo.h"
30#include "WebContext.h"
31#include "WebFormSubmissionListenerProxy.h"
32#include "WebFramePolicyListenerProxy.h"
33#include "WebPageMessages.h"
34#include "WebPageProxy.h"
35#include <WebCore/DOMImplementation.h>
36#include <WebCore/Image.h>
37#include <WebCore/MIMETypeRegistry.h>
38#include <stdio.h>
39#include <wtf/text/WTFString.h>
40
41using namespace WebCore;
42
43namespace WebKit {
44
45WebFrameProxy::WebFrameProxy(WebPageProxy* page, uint64_t frameID)
46    : m_page(page)
47    , m_loadState(LoadStateFinished)
48    , m_isFrameSet(false)
49    , m_frameID(frameID)
50{
51    WebContext::statistics().wkFrameCount++;
52}
53
54WebFrameProxy::~WebFrameProxy()
55{
56    WebContext::statistics().wkFrameCount--;
57}
58
59void WebFrameProxy::disconnect()
60{
61    m_page = 0;
62
63    if (m_activeListener) {
64        m_activeListener->invalidate();
65        m_activeListener = 0;
66    }
67}
68
69bool WebFrameProxy::isMainFrame() const
70{
71    if (!m_page)
72        return false;
73
74    return this == m_page->mainFrame();
75}
76
77void WebFrameProxy::stopLoading() const
78{
79    if (!m_page)
80        return;
81
82    if (!m_page->isValid())
83        return;
84
85    m_page->process()->send(Messages::WebPage::StopLoadingFrame(m_frameID), m_page->pageID());
86}
87
88bool WebFrameProxy::canProvideSource() const
89{
90    return isDisplayingMarkupDocument();
91}
92
93bool WebFrameProxy::canShowMIMEType(const String& mimeType) const
94{
95    if (!m_page)
96        return false;
97
98    return m_page->canShowMIMEType(mimeType);
99}
100
101bool WebFrameProxy::isDisplayingStandaloneImageDocument() const
102{
103    return Image::supportsType(m_MIMEType);
104}
105
106bool WebFrameProxy::isDisplayingMarkupDocument() const
107{
108    // FIXME: This check should be moved to somewhere in WebCore.
109    return m_MIMEType == "text/html" || m_MIMEType == "image/svg+xml" || m_MIMEType == "application/x-webarchive" || DOMImplementation::isXMLMIMEType(m_MIMEType);
110}
111
112bool WebFrameProxy::isDisplayingPDFDocument() const
113{
114    if (m_MIMEType.isEmpty())
115        return false;
116
117    return MIMETypeRegistry::isPDFOrPostScriptMIMEType(m_MIMEType);
118}
119
120void WebFrameProxy::didStartProvisionalLoad(const String& url)
121{
122    ASSERT(m_provisionalURL.isEmpty());
123    m_loadState = LoadStateProvisional;
124    m_provisionalURL = url;
125}
126
127void WebFrameProxy::didReceiveServerRedirectForProvisionalLoad(const String& url)
128{
129    ASSERT(m_loadState == LoadStateProvisional);
130    m_provisionalURL = url;
131}
132
133void WebFrameProxy::didFailProvisionalLoad()
134{
135    ASSERT(m_loadState == LoadStateProvisional);
136    m_loadState = LoadStateFinished;
137    m_provisionalURL = String();
138    m_unreachableURL = m_lastUnreachableURL;
139}
140
141void WebFrameProxy::didCommitLoad(const String& contentType, const PlatformCertificateInfo& certificateInfo)
142{
143    ASSERT(m_loadState == LoadStateProvisional);
144    m_loadState = LoadStateCommitted;
145    m_url = m_provisionalURL;
146    m_provisionalURL = String();
147    m_title = String();
148    m_MIMEType = contentType;
149    m_isFrameSet = false;
150    m_certificateInfo = WebCertificateInfo::create(certificateInfo);
151}
152
153void WebFrameProxy::didFinishLoad()
154{
155    ASSERT(m_loadState == LoadStateCommitted);
156    ASSERT(m_provisionalURL.isEmpty());
157    m_loadState = LoadStateFinished;
158}
159
160void WebFrameProxy::didFailLoad()
161{
162    ASSERT(m_loadState == LoadStateCommitted);
163    ASSERT(m_provisionalURL.isEmpty());
164    m_loadState = LoadStateFinished;
165}
166
167void WebFrameProxy::didSameDocumentNavigation(const String& url)
168{
169    m_url = url;
170}
171
172void WebFrameProxy::didChangeTitle(const String& title)
173{
174    m_title = title;
175}
176
177void WebFrameProxy::receivedPolicyDecision(WebCore::PolicyAction action, uint64_t listenerID)
178{
179    if (!m_page)
180        return;
181
182    ASSERT(m_activeListener);
183    ASSERT(m_activeListener->listenerID() == listenerID);
184    m_page->receivedPolicyDecision(action, this, listenerID);
185}
186
187WebFramePolicyListenerProxy* WebFrameProxy::setUpPolicyListenerProxy(uint64_t listenerID)
188{
189    if (m_activeListener)
190        m_activeListener->invalidate();
191    m_activeListener = WebFramePolicyListenerProxy::create(this, listenerID);
192    return static_cast<WebFramePolicyListenerProxy*>(m_activeListener.get());
193}
194
195WebFormSubmissionListenerProxy* WebFrameProxy::setUpFormSubmissionListenerProxy(uint64_t listenerID)
196{
197    if (m_activeListener)
198        m_activeListener->invalidate();
199    m_activeListener = WebFormSubmissionListenerProxy::create(this, listenerID);
200    return static_cast<WebFormSubmissionListenerProxy*>(m_activeListener.get());
201}
202
203void WebFrameProxy::getWebArchive(PassRefPtr<DataCallback> callback)
204{
205    if (!m_page) {
206        callback->invalidate();
207        return;
208    }
209
210    m_page->getWebArchiveOfFrame(this, callback);
211}
212
213void WebFrameProxy::getMainResourceData(PassRefPtr<DataCallback> callback)
214{
215    if (!m_page) {
216        callback->invalidate();
217        return;
218    }
219
220    m_page->getMainResourceDataOfFrame(this, callback);
221}
222
223void WebFrameProxy::getResourceData(WebURL* resourceURL, PassRefPtr<DataCallback> callback)
224{
225    if (!m_page) {
226        callback->invalidate();
227        return;
228    }
229
230    m_page->getResourceDataFromFrame(this, resourceURL, callback);
231}
232
233void WebFrameProxy::setUnreachableURL(const String& unreachableURL)
234{
235    m_lastUnreachableURL = m_unreachableURL;
236    m_unreachableURL = unreachableURL;
237}
238
239} // namespace WebKit
240