Merge "Use EwkView's variables instead of drawingScaleFactor and drawingScrollPositio...
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / WebFrameProxy.cpp
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 <stdio.h>
38 #include <wtf/text/WTFString.h>
39
40 using namespace WebCore;
41 using namespace std;
42
43 namespace WebKit {
44
45 WebFrameProxy::WebFrameProxy(WebPageProxy* page, uint64_t frameID)
46     : m_page(page)
47     , m_parentFrame(0)
48     , m_nextSibling(0)
49     , m_previousSibling(0)
50     , m_firstChild(0)
51     , m_lastChild(0)
52     , m_loadState(LoadStateFinished)
53     , m_isFrameSet(false)
54     , m_frameID(frameID)
55 {
56     WebContext::statistics().wkFrameCount++;
57 }
58
59 WebFrameProxy::~WebFrameProxy()
60 {
61     WebContext::statistics().wkFrameCount--;
62 }
63
64 void WebFrameProxy::disconnect()
65 {
66     m_page = 0;
67     m_parentFrame = 0;
68     m_nextSibling = 0;
69     m_previousSibling = 0;
70     m_firstChild = 0;
71     m_lastChild = 0;
72
73     if (m_activeListener) {
74         m_activeListener->invalidate();
75         m_activeListener = 0;
76     }
77 }
78
79 bool WebFrameProxy::isMainFrame() const
80 {
81     if (!m_page)
82         return false;
83
84     return this == m_page->mainFrame();
85 }
86
87 void WebFrameProxy::stopLoading() const
88 {
89     if (!m_page)
90         return;
91
92     if (!m_page->isValid())
93         return;
94
95     m_page->process()->send(Messages::WebPage::StopLoadingFrame(m_frameID), m_page->pageID());
96 }
97     
98 bool WebFrameProxy::canProvideSource() const
99 {
100     return isDisplayingMarkupDocument();
101 }
102
103 bool WebFrameProxy::canShowMIMEType(const String& mimeType) const
104 {
105     if (!m_page)
106         return false;
107
108     if (m_page->canShowMIMEType(mimeType))
109         return true;
110
111 #if PLATFORM(MAC)
112     // On Mac, we can show PDFs.
113     if (!mimeType.isEmpty())
114         return WebContext::pdfAndPostScriptMIMETypes().contains(mimeType) && !WebContext::omitPDFSupport();
115 #endif
116
117     return false;
118 }
119
120 bool WebFrameProxy::isDisplayingStandaloneImageDocument() const
121 {
122     return Image::supportsType(m_MIMEType);
123 }
124
125 bool WebFrameProxy::isDisplayingMarkupDocument() const
126 {
127     // FIXME: This check should be moved to somewhere in WebCore.
128     // FIXME: This returns false when displaying a web archive.
129     return m_MIMEType == "text/html" || m_MIMEType == "image/svg+xml" || DOMImplementation::isXMLMIMEType(m_MIMEType);
130 }
131
132 bool WebFrameProxy::isDisplayingPDFDocument() const
133 {
134     if (m_MIMEType.isEmpty())
135         return false;
136
137     return WebContext::pdfAndPostScriptMIMETypes().contains(m_MIMEType);
138 }
139
140 void WebFrameProxy::didStartProvisionalLoad(const String& url)
141 {
142     ASSERT(m_loadState == LoadStateFinished);
143     ASSERT(m_provisionalURL.isEmpty());
144     m_loadState = LoadStateProvisional;
145     m_provisionalURL = url;
146 }
147
148 void WebFrameProxy::didReceiveServerRedirectForProvisionalLoad(const String& url)
149 {
150     ASSERT(m_loadState == LoadStateProvisional);
151     m_provisionalURL = url;
152 }
153
154 void WebFrameProxy::didFailProvisionalLoad()
155 {
156     ASSERT(m_loadState == LoadStateProvisional);
157     m_loadState = LoadStateFinished;
158     m_provisionalURL = String();
159     m_unreachableURL = m_lastUnreachableURL;
160 }
161
162 void WebFrameProxy::didCommitLoad(const String& contentType, const PlatformCertificateInfo& certificateInfo)
163 {
164     ASSERT(m_loadState == LoadStateProvisional);
165     m_loadState = LoadStateCommitted;
166     m_url = m_provisionalURL;
167     m_provisionalURL = String();
168     m_title = String();
169     m_MIMEType = contentType;
170     m_isFrameSet = false;
171     m_certificateInfo = WebCertificateInfo::create(certificateInfo);
172 }
173
174 void WebFrameProxy::didFinishLoad()
175 {
176     ASSERT(m_loadState == LoadStateCommitted);
177     ASSERT(m_provisionalURL.isEmpty());
178     m_loadState = LoadStateFinished;
179 }
180
181 void WebFrameProxy::didFailLoad()
182 {
183     ASSERT(m_loadState == LoadStateCommitted);
184     ASSERT(m_provisionalURL.isEmpty());
185     m_loadState = LoadStateFinished;
186     m_title = String();
187 }
188
189 void WebFrameProxy::didSameDocumentNavigation(const String& url)
190 {
191     m_url = url;
192 }
193
194 void WebFrameProxy::didChangeTitle(const String& title)
195 {
196     m_title = title;
197 }
198
199 void WebFrameProxy::appendChild(WebFrameProxy* child)
200 {
201     ASSERT(child->page() == page());
202     ASSERT(!child->m_parentFrame);
203     ASSERT(!child->m_nextSibling);
204     ASSERT(!child->m_previousSibling);
205
206     child->m_parentFrame = this;
207
208     WebFrameProxy* oldLast = m_lastChild;
209     m_lastChild = child;
210
211     if (oldLast) {
212         ASSERT(!oldLast->m_nextSibling);
213         child->m_previousSibling = oldLast;
214         oldLast->m_nextSibling = child;
215     } else
216         m_firstChild = child;
217 }
218
219 void WebFrameProxy::removeChild(WebFrameProxy* child)
220 {
221     child->m_parentFrame = 0;
222
223     WebFrameProxy*& newLocationForNext = m_firstChild == child ? m_firstChild : child->m_previousSibling->m_nextSibling;
224     WebFrameProxy*& newLocationForPrevious = m_lastChild == child ? m_lastChild : child->m_nextSibling->m_previousSibling;
225     swap(newLocationForNext, child->m_nextSibling);
226     swap(newLocationForPrevious, child->m_previousSibling);
227     child->m_previousSibling = 0;
228     child->m_nextSibling = 0;
229 }
230
231 bool WebFrameProxy::isDescendantOf(const WebFrameProxy* ancestor) const
232 {
233     if (!ancestor)
234         return false;
235
236     if (m_page != ancestor->m_page)
237         return false;
238
239     for (const WebFrameProxy* frame = this; frame; frame = frame->m_parentFrame) {
240         if (frame == ancestor)
241             return true;
242     }
243
244     return false;
245 }
246
247 void WebFrameProxy::dumpFrameTreeToSTDOUT(unsigned indent)
248 {
249     if (!indent && m_parentFrame)
250         printf("NOTE: Printing subtree.\n");
251
252     for (unsigned i = 0; i < indent; ++i)
253         printf(" ");
254     printf("| FRAME %d %s\n", (int)m_frameID, m_url.utf8().data());
255
256     for (WebFrameProxy* child = m_firstChild; child; child = child->m_nextSibling)
257         child->dumpFrameTreeToSTDOUT(indent + 4);
258 }
259
260 void WebFrameProxy::didRemoveFromHierarchy()
261 {
262     if (m_parentFrame)
263         m_parentFrame->removeChild(this);
264 }
265
266 PassRefPtr<ImmutableArray> WebFrameProxy::childFrames()
267 {
268     if (!m_firstChild)
269         return ImmutableArray::create();
270
271     Vector<RefPtr<APIObject> > vector;
272     for (WebFrameProxy* child = m_firstChild; child; child = child->m_nextSibling)
273         vector.append(child);
274
275     return ImmutableArray::adopt(vector);
276 }
277
278 void WebFrameProxy::receivedPolicyDecision(WebCore::PolicyAction action, uint64_t listenerID)
279 {
280     if (!m_page)
281         return;
282
283     ASSERT(m_activeListener);
284     ASSERT(m_activeListener->listenerID() == listenerID);
285     m_page->receivedPolicyDecision(action, this, listenerID);
286 }
287
288 WebFramePolicyListenerProxy* WebFrameProxy::setUpPolicyListenerProxy(uint64_t listenerID)
289 {
290     if (m_activeListener)
291         m_activeListener->invalidate();
292     m_activeListener = WebFramePolicyListenerProxy::create(this, listenerID);
293     return static_cast<WebFramePolicyListenerProxy*>(m_activeListener.get());
294 }
295
296 WebFormSubmissionListenerProxy* WebFrameProxy::setUpFormSubmissionListenerProxy(uint64_t listenerID)
297 {
298     if (m_activeListener)
299         m_activeListener->invalidate();
300     m_activeListener = WebFormSubmissionListenerProxy::create(this, listenerID);
301     return static_cast<WebFormSubmissionListenerProxy*>(m_activeListener.get());
302 }
303
304 #if ENABLE(TIZEN_ON_AUTHENTICATION_REQUESTED)
305 AuthenticationChallengeProxy* WebFrameProxy::setUpAuthenticationChallengeProxy(const WebCore::AuthenticationChallenge& authenticationChallenge, uint64_t challengeID, WebProcessProxy* process)
306 {
307     // FIXME : If the authenticationChallenge is overlapped by another request before receiving auth confirm or cancel message, m_activeAuthenticationChallenge is not safe anymore.
308     m_activeAuthenticationChallenge = AuthenticationChallengeProxy::create(authenticationChallenge, challengeID, process);
309     return static_cast<AuthenticationChallengeProxy*>(m_activeAuthenticationChallenge.get());
310 }
311 #endif
312
313 void WebFrameProxy::getWebArchive(PassRefPtr<DataCallback> callback)
314 {
315     if (!m_page) {
316         callback->invalidate();
317         return;
318     }
319
320     m_page->getWebArchiveOfFrame(this, callback);
321 }
322
323 void WebFrameProxy::getMainResourceData(PassRefPtr<DataCallback> callback)
324 {
325     if (!m_page) {
326         callback->invalidate();
327         return;
328     }
329
330     m_page->getMainResourceDataOfFrame(this, callback);
331 }
332
333 void WebFrameProxy::getResourceData(WebURL* resourceURL, PassRefPtr<DataCallback> callback)
334 {
335     if (!m_page) {
336         callback->invalidate();
337         return;
338     }
339
340     m_page->getResourceDataFromFrame(this, resourceURL, callback);
341 }
342
343 void WebFrameProxy::setUnreachableURL(const String& unreachableURL)
344 {
345     m_lastUnreachableURL = m_unreachableURL;
346     m_unreachableURL = unreachableURL;
347 }
348
349 } // namespace WebKit