[Qt][WK2] Move non-api classes to WebKit namespace at WebKit2/UiProcess/qt
[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 "QtWebError.h"
25 #include "WKStringQt.h"
26 #include "qquickwebview_p.h"
27 #include "qquickwebview_p_p.h"
28 #include "qwebloadrequest_p.h"
29 #include <WKFrame.h>
30
31 namespace WebKit {
32
33 QtWebPageLoadClient::QtWebPageLoadClient(WKPageRef pageRef, QQuickWebView* webView)
34     : m_webView(webView)
35     , m_loadProgress(0)
36 {
37     WKPageLoaderClient loadClient;
38     memset(&loadClient, 0, sizeof(WKPageLoaderClient));
39     loadClient.version = kWKPageLoaderClientCurrentVersion;
40     loadClient.clientInfo = this;
41     loadClient.didStartProvisionalLoadForFrame = didStartProvisionalLoadForFrame;
42     loadClient.didFailProvisionalLoadWithErrorForFrame = didFailProvisionalLoadWithErrorForFrame;
43     loadClient.didCommitLoadForFrame = didCommitLoadForFrame;
44     loadClient.didFinishLoadForFrame = didFinishLoadForFrame;
45     loadClient.didFailLoadWithErrorForFrame = didFailLoadWithErrorForFrame;
46     loadClient.didSameDocumentNavigationForFrame = didSameDocumentNavigationForFrame;
47     loadClient.didReceiveTitleForFrame = didReceiveTitleForFrame;
48     loadClient.didStartProgress = didStartProgress;
49     loadClient.didChangeProgress = didChangeProgress;
50     loadClient.didFinishProgress = didFinishProgress;
51     loadClient.didFirstVisuallyNonEmptyLayoutForFrame = didFirstVisuallyNonEmptyLayoutForFrame;
52     loadClient.didChangeBackForwardList = didChangeBackForwardList;
53     WKPageSetPageLoaderClient(pageRef, &loadClient);
54 }
55
56 void QtWebPageLoadClient::didStartProvisionalLoadForFrame(const QUrl& url)
57 {
58     QWebLoadRequest loadRequest(url, QQuickWebView::LoadStartedStatus);
59     m_webView->d_func()->didChangeLoadingState(&loadRequest);
60 }
61
62 void QtWebPageLoadClient::didCommitLoadForFrame()
63 {
64     emit m_webView->navigationHistoryChanged();
65     emit m_webView->urlChanged();
66     emit m_webView->titleChanged();
67     m_webView->d_func()->loadDidCommit();
68 }
69
70 void QtWebPageLoadClient::didSameDocumentNavigationForFrame()
71 {
72     emit m_webView->navigationHistoryChanged();
73     emit m_webView->urlChanged();
74 }
75
76 void QtWebPageLoadClient::didReceiveTitleForFrame()
77 {
78     emit m_webView->titleChanged();
79 }
80
81 void QtWebPageLoadClient::didFirstVisuallyNonEmptyLayoutForFrame()
82 {
83     m_webView->d_func()->didFinishFirstNonEmptyLayout();
84 }
85
86 void QtWebPageLoadClient::didChangeBackForwardList()
87 {
88     m_webView->d_func()->didChangeBackForwardList();
89 }
90
91 void QtWebPageLoadClient::dispatchLoadSucceeded()
92 {
93     m_webView->d_func()->loadDidSucceed();
94 }
95
96 void QtWebPageLoadClient::dispatchLoadFailed(WKErrorRef error)
97 {
98     int errorCode = WKErrorGetErrorCode(error);
99     if (toImpl(error)->platformError().isCancellation() || errorCode == kWKErrorCodeFrameLoadInterruptedByPolicyChange || errorCode == kWKErrorCodePlugInWillHandleLoad) {
100         // Make sure that LoadStartedStatus has a counterpart when e.g. requesting a download.
101         dispatchLoadSucceeded();
102         return;
103     }
104
105     QtWebError qtError(error);
106     QWebLoadRequest loadRequest(qtError.url(), QQuickWebView::LoadFailedStatus, qtError.description(), static_cast<QQuickWebView::ErrorDomain>(qtError.type()), qtError.errorCode());
107     emit m_webView->loadingChanged(&loadRequest);
108 }
109
110 void QtWebPageLoadClient::setLoadProgress(int loadProgress)
111 {
112     m_loadProgress = loadProgress;
113     emit m_webView->loadProgressChanged();
114 }
115
116 static QtWebPageLoadClient* toQtWebPageLoadClient(const void* clientInfo)
117 {
118     ASSERT(clientInfo);
119     return reinterpret_cast<QtWebPageLoadClient*>(const_cast<void*>(clientInfo));
120 }
121
122 void QtWebPageLoadClient::didStartProvisionalLoadForFrame(WKPageRef, WKFrameRef frame, WKTypeRef, const void* clientInfo)
123 {
124     if (!WKFrameIsMainFrame(frame))
125         return;
126     WebFrameProxy* wkframe = toImpl(frame);
127     QString urlStr(wkframe->provisionalURL());
128     QUrl qUrl = urlStr;
129     toQtWebPageLoadClient(clientInfo)->didStartProvisionalLoadForFrame(qUrl);
130 }
131
132 void QtWebPageLoadClient::didFailProvisionalLoadWithErrorForFrame(WKPageRef, WKFrameRef frame, WKErrorRef error, WKTypeRef, const void* clientInfo)
133 {
134     if (!WKFrameIsMainFrame(frame))
135         return;
136     toQtWebPageLoadClient(clientInfo)->dispatchLoadFailed(error);
137 }
138
139 void QtWebPageLoadClient::didCommitLoadForFrame(WKPageRef, WKFrameRef frame, WKTypeRef, const void* clientInfo)
140 {
141     if (!WKFrameIsMainFrame(frame))
142         return;
143     toQtWebPageLoadClient(clientInfo)->didCommitLoadForFrame();
144 }
145
146 void QtWebPageLoadClient::didFinishLoadForFrame(WKPageRef, WKFrameRef frame, WKTypeRef, const void* clientInfo)
147 {
148     if (!WKFrameIsMainFrame(frame))
149         return;
150     toQtWebPageLoadClient(clientInfo)->dispatchLoadSucceeded();
151 }
152
153 void QtWebPageLoadClient::didFailLoadWithErrorForFrame(WKPageRef, WKFrameRef frame, WKErrorRef error, WKTypeRef, const void* clientInfo)
154 {
155     if (!WKFrameIsMainFrame(frame))
156         return;
157     toQtWebPageLoadClient(clientInfo)->dispatchLoadFailed(error);
158 }
159
160 void QtWebPageLoadClient::didSameDocumentNavigationForFrame(WKPageRef page, WKFrameRef frame, WKSameDocumentNavigationType type, WKTypeRef userData, const void* clientInfo)
161 {
162     toQtWebPageLoadClient(clientInfo)->didSameDocumentNavigationForFrame();
163 }
164
165 void QtWebPageLoadClient::didReceiveTitleForFrame(WKPageRef, WKStringRef title, WKFrameRef frame, WKTypeRef, const void* clientInfo)
166 {
167     if (!WKFrameIsMainFrame(frame))
168         return;
169     toQtWebPageLoadClient(clientInfo)->didReceiveTitleForFrame();
170 }
171
172 void QtWebPageLoadClient::didStartProgress(WKPageRef, const void* clientInfo)
173 {
174     QtWebPageLoadClient* client = toQtWebPageLoadClient(clientInfo);
175     client->setLoadProgress(0);
176     client->m_webView->d_func()->setIcon(QUrl());
177 }
178
179 void QtWebPageLoadClient::didChangeProgress(WKPageRef page, const void* clientInfo)
180 {
181     toQtWebPageLoadClient(clientInfo)->setLoadProgress(WKPageGetEstimatedProgress(page) * 100);
182 }
183
184 void QtWebPageLoadClient::didFinishProgress(WKPageRef, const void* clientInfo)
185 {
186     toQtWebPageLoadClient(clientInfo)->setLoadProgress(100);
187 }
188
189 void QtWebPageLoadClient::didFirstVisuallyNonEmptyLayoutForFrame(WKPageRef, WKFrameRef frame, WKTypeRef, const void *clientInfo)
190 {
191     if (!WKFrameIsMainFrame(frame))
192         return;
193     toQtWebPageLoadClient(clientInfo)->didFirstVisuallyNonEmptyLayoutForFrame();
194 }
195
196 void QtWebPageLoadClient::didChangeBackForwardList(WKPageRef, WKBackForwardListItemRef, WKArrayRef, const void *clientInfo)
197 {
198     toQtWebPageLoadClient(clientInfo)->didChangeBackForwardList();
199 }
200
201 } // namespace Webkit