a2480e370bf91ef71deaff320724e357e38e58d4
[WebKit-https.git] / Source / WebKit2 / UIProcess / qt / QtWebPageLoadClient.cpp
1 /*
2  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20
21 #include "config.h"
22 #include "QtWebPageLoadClient.h"
23
24 #include "WKStringQt.h"
25 #include "qquickwebview_p.h"
26 #include "qquickwebview_p_p.h"
27 #include <WKFrame.h>
28
29 QtWebPageLoadClient::QtWebPageLoadClient(WKPageRef pageRef, QQuickWebView* webView)
30     : m_webView(webView)
31     , m_loadProgress(0)
32 {
33     WKPageLoaderClient loadClient;
34     memset(&loadClient, 0, sizeof(WKPageLoaderClient));
35     loadClient.version = kWKPageLoaderClientCurrentVersion;
36     loadClient.clientInfo = this;
37     loadClient.didStartProvisionalLoadForFrame = didStartProvisionalLoadForFrame;
38     loadClient.didFailProvisionalLoadWithErrorForFrame = didFailProvisionalLoadWithErrorForFrame;
39     loadClient.didCommitLoadForFrame = didCommitLoadForFrame;
40     loadClient.didFinishLoadForFrame = didFinishLoadForFrame;
41     loadClient.didFailLoadWithErrorForFrame = didFailLoadWithErrorForFrame;
42     loadClient.didSameDocumentNavigationForFrame = didSameDocumentNavigationForFrame;
43     loadClient.didReceiveTitleForFrame = didReceiveTitleForFrame;
44     loadClient.didStartProgress = didStartProgress;
45     loadClient.didChangeProgress = didChangeProgress;
46     loadClient.didFinishProgress = didFinishProgress;
47     loadClient.didFirstVisuallyNonEmptyLayoutForFrame = didFirstVisuallyNonEmptyLayoutForFrame;
48     WKPageSetPageLoaderClient(pageRef, &loadClient);
49 }
50
51 void QtWebPageLoadClient::didStartProvisionalLoadForFrame()
52 {
53     emit m_webView->navigationStateChanged();
54     emit m_webView->loadStarted();
55 }
56
57 void QtWebPageLoadClient::didCommitLoadForFrame(const QUrl& url)
58 {
59     emit m_webView->navigationStateChanged();
60     emit m_webView->urlChanged(url);
61     m_webView->d_func()->loadDidCommit();
62 }
63
64 void QtWebPageLoadClient::didSameDocumentNavigationForFrame(const QUrl& url)
65 {
66     emit m_webView->navigationStateChanged();
67     emit m_webView->urlChanged(url);
68 }
69
70 void QtWebPageLoadClient::didReceiveTitleForFrame(const QString& title)
71 {
72     emit m_webView->titleChanged(title);
73 }
74
75 void QtWebPageLoadClient::didFirstVisuallyNonEmptyLayoutForFrame()
76 {
77     m_webView->d_func()->didFinishFirstNonEmptyLayout();
78 }
79
80 void QtWebPageLoadClient::dispatchLoadSucceeded()
81 {
82     emit m_webView->navigationStateChanged();
83     emit m_webView->loadSucceeded();
84 }
85
86 void QtWebPageLoadClient::dispatchLoadFailed(WKErrorRef error)
87 {
88     emit m_webView->navigationStateChanged();
89
90     int errorCode = WKErrorGetErrorCode(error);
91     if (toImpl(error)->platformError().isCancellation() || errorCode == kWKErrorCodeFrameLoadInterruptedByPolicyChange || errorCode == kWKErrorCodePlugInWillHandleLoad)
92         return;
93
94     QtWebError qtError(error);
95     emit m_webView->loadFailed(static_cast<QQuickWebView::ErrorType>(qtError.type()), qtError.errorCode(), qtError.url());
96 }
97
98 void QtWebPageLoadClient::setLoadProgress(int loadProgress)
99 {
100     m_loadProgress = loadProgress;
101     emit m_webView->loadProgressChanged(m_loadProgress);
102 }
103
104 static QtWebPageLoadClient* toQtWebPageLoadClient(const void* clientInfo)
105 {
106     ASSERT(clientInfo);
107     return reinterpret_cast<QtWebPageLoadClient*>(const_cast<void*>(clientInfo));
108 }
109
110 void QtWebPageLoadClient::didStartProvisionalLoadForFrame(WKPageRef, WKFrameRef frame, WKTypeRef, const void* clientInfo)
111 {
112     if (!WKFrameIsMainFrame(frame))
113         return;
114     toQtWebPageLoadClient(clientInfo)->didStartProvisionalLoadForFrame();
115 }
116
117 void QtWebPageLoadClient::didFailProvisionalLoadWithErrorForFrame(WKPageRef, WKFrameRef frame, WKErrorRef error, WKTypeRef, const void* clientInfo)
118 {
119     if (!WKFrameIsMainFrame(frame))
120         return;
121     toQtWebPageLoadClient(clientInfo)->dispatchLoadFailed(error);
122 }
123
124 void QtWebPageLoadClient::didCommitLoadForFrame(WKPageRef, WKFrameRef frame, WKTypeRef, const void* clientInfo)
125 {
126     if (!WKFrameIsMainFrame(frame))
127         return;
128     WebFrameProxy* wkframe = toImpl(frame);
129     QString urlStr(wkframe->url());
130     QUrl qUrl = urlStr;
131     toQtWebPageLoadClient(clientInfo)->didCommitLoadForFrame(qUrl);
132 }
133
134 void QtWebPageLoadClient::didFinishLoadForFrame(WKPageRef, WKFrameRef frame, WKTypeRef, const void* clientInfo)
135 {
136     if (!WKFrameIsMainFrame(frame))
137         return;
138     toQtWebPageLoadClient(clientInfo)->dispatchLoadSucceeded();
139 }
140
141 void QtWebPageLoadClient::didFailLoadWithErrorForFrame(WKPageRef, WKFrameRef frame, WKErrorRef error, WKTypeRef, const void* clientInfo)
142 {
143     if (!WKFrameIsMainFrame(frame))
144         return;
145     toQtWebPageLoadClient(clientInfo)->dispatchLoadFailed(error);
146 }
147
148 void QtWebPageLoadClient::didSameDocumentNavigationForFrame(WKPageRef page, WKFrameRef frame, WKSameDocumentNavigationType type, WKTypeRef userData, const void* clientInfo)
149 {
150     WebFrameProxy* wkframe = toImpl(frame);
151     QString urlStr(wkframe->url());
152     QUrl qUrl = urlStr;
153     toQtWebPageLoadClient(clientInfo)->didSameDocumentNavigationForFrame(qUrl);
154 }
155
156 void QtWebPageLoadClient::didReceiveTitleForFrame(WKPageRef, WKStringRef title, WKFrameRef frame, WKTypeRef, const void* clientInfo)
157 {
158     if (!WKFrameIsMainFrame(frame))
159         return;
160     QString qTitle = WKStringCopyQString(title);
161     toQtWebPageLoadClient(clientInfo)->didReceiveTitleForFrame(qTitle);
162 }
163
164 void QtWebPageLoadClient::didStartProgress(WKPageRef, const void* clientInfo)
165 {
166     toQtWebPageLoadClient(clientInfo)->setLoadProgress(0);
167 }
168
169 void QtWebPageLoadClient::didChangeProgress(WKPageRef page, const void* clientInfo)
170 {
171     toQtWebPageLoadClient(clientInfo)->setLoadProgress(WKPageGetEstimatedProgress(page) * 100);
172 }
173
174 void QtWebPageLoadClient::didFinishProgress(WKPageRef, const void* clientInfo)
175 {
176     toQtWebPageLoadClient(clientInfo)->setLoadProgress(100);
177 }
178
179 void QtWebPageLoadClient::didFirstVisuallyNonEmptyLayoutForFrame(WKPageRef, WKFrameRef frame, WKTypeRef, const void *clientInfo)
180 {
181     if (!WKFrameIsMainFrame(frame))
182         return;
183     toQtWebPageLoadClient(clientInfo)->didFirstVisuallyNonEmptyLayoutForFrame();
184 }