[Qt][WK2] History is not accessible in QML.
[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     loadClient.didChangeBackForwardList = didChangeBackForwardList;
49     WKPageSetPageLoaderClient(pageRef, &loadClient);
50 }
51
52 void QtWebPageLoadClient::didStartProvisionalLoadForFrame()
53 {
54     emit m_webView->navigationStateChanged();
55     emit m_webView->loadStarted();
56 }
57
58 void QtWebPageLoadClient::didCommitLoadForFrame(const QUrl& url)
59 {
60     emit m_webView->navigationStateChanged();
61     emit m_webView->urlChanged(url);
62     m_webView->d_func()->loadDidCommit();
63 }
64
65 void QtWebPageLoadClient::didSameDocumentNavigationForFrame(const QUrl& url)
66 {
67     emit m_webView->navigationStateChanged();
68     emit m_webView->urlChanged(url);
69 }
70
71 void QtWebPageLoadClient::didReceiveTitleForFrame(const QString& title)
72 {
73     emit m_webView->titleChanged(title);
74 }
75
76 void QtWebPageLoadClient::didFirstVisuallyNonEmptyLayoutForFrame()
77 {
78     m_webView->d_func()->didFinishFirstNonEmptyLayout();
79 }
80
81 void QtWebPageLoadClient::didChangeBackForwardList()
82 {
83     m_webView->d_func()->didChangeBackForwardList();
84 }
85
86 void QtWebPageLoadClient::dispatchLoadSucceeded()
87 {
88     emit m_webView->navigationStateChanged();
89     emit m_webView->loadSucceeded();
90 }
91
92 void QtWebPageLoadClient::dispatchLoadFailed(WKErrorRef error)
93 {
94     emit m_webView->navigationStateChanged();
95
96     int errorCode = WKErrorGetErrorCode(error);
97     if (toImpl(error)->platformError().isCancellation() || errorCode == kWKErrorCodeFrameLoadInterruptedByPolicyChange || errorCode == kWKErrorCodePlugInWillHandleLoad)
98         return;
99
100     QtWebError qtError(error);
101     emit m_webView->loadFailed(static_cast<QQuickWebView::ErrorType>(qtError.type()), qtError.errorCode(), qtError.url());
102 }
103
104 void QtWebPageLoadClient::setLoadProgress(int loadProgress)
105 {
106     m_loadProgress = loadProgress;
107     emit m_webView->loadProgressChanged(m_loadProgress);
108 }
109
110 static QtWebPageLoadClient* toQtWebPageLoadClient(const void* clientInfo)
111 {
112     ASSERT(clientInfo);
113     return reinterpret_cast<QtWebPageLoadClient*>(const_cast<void*>(clientInfo));
114 }
115
116 void QtWebPageLoadClient::didStartProvisionalLoadForFrame(WKPageRef, WKFrameRef frame, WKTypeRef, const void* clientInfo)
117 {
118     if (!WKFrameIsMainFrame(frame))
119         return;
120     toQtWebPageLoadClient(clientInfo)->didStartProvisionalLoadForFrame();
121 }
122
123 void QtWebPageLoadClient::didFailProvisionalLoadWithErrorForFrame(WKPageRef, WKFrameRef frame, WKErrorRef error, WKTypeRef, const void* clientInfo)
124 {
125     if (!WKFrameIsMainFrame(frame))
126         return;
127     toQtWebPageLoadClient(clientInfo)->dispatchLoadFailed(error);
128 }
129
130 void QtWebPageLoadClient::didCommitLoadForFrame(WKPageRef, WKFrameRef frame, WKTypeRef, const void* clientInfo)
131 {
132     if (!WKFrameIsMainFrame(frame))
133         return;
134     WebFrameProxy* wkframe = toImpl(frame);
135     QString urlStr(wkframe->url());
136     QUrl qUrl = urlStr;
137     toQtWebPageLoadClient(clientInfo)->didCommitLoadForFrame(qUrl);
138 }
139
140 void QtWebPageLoadClient::didFinishLoadForFrame(WKPageRef, WKFrameRef frame, WKTypeRef, const void* clientInfo)
141 {
142     if (!WKFrameIsMainFrame(frame))
143         return;
144     toQtWebPageLoadClient(clientInfo)->dispatchLoadSucceeded();
145 }
146
147 void QtWebPageLoadClient::didFailLoadWithErrorForFrame(WKPageRef, WKFrameRef frame, WKErrorRef error, WKTypeRef, const void* clientInfo)
148 {
149     if (!WKFrameIsMainFrame(frame))
150         return;
151     toQtWebPageLoadClient(clientInfo)->dispatchLoadFailed(error);
152 }
153
154 void QtWebPageLoadClient::didSameDocumentNavigationForFrame(WKPageRef page, WKFrameRef frame, WKSameDocumentNavigationType type, WKTypeRef userData, const void* clientInfo)
155 {
156     WebFrameProxy* wkframe = toImpl(frame);
157     QString urlStr(wkframe->url());
158     QUrl qUrl = urlStr;
159     toQtWebPageLoadClient(clientInfo)->didSameDocumentNavigationForFrame(qUrl);
160 }
161
162 void QtWebPageLoadClient::didReceiveTitleForFrame(WKPageRef, WKStringRef title, WKFrameRef frame, WKTypeRef, const void* clientInfo)
163 {
164     if (!WKFrameIsMainFrame(frame))
165         return;
166     QString qTitle = WKStringCopyQString(title);
167     toQtWebPageLoadClient(clientInfo)->didReceiveTitleForFrame(qTitle);
168 }
169
170 void QtWebPageLoadClient::didStartProgress(WKPageRef, const void* clientInfo)
171 {
172     toQtWebPageLoadClient(clientInfo)->setLoadProgress(0);
173 }
174
175 void QtWebPageLoadClient::didChangeProgress(WKPageRef page, const void* clientInfo)
176 {
177     toQtWebPageLoadClient(clientInfo)->setLoadProgress(WKPageGetEstimatedProgress(page) * 100);
178 }
179
180 void QtWebPageLoadClient::didFinishProgress(WKPageRef, const void* clientInfo)
181 {
182     toQtWebPageLoadClient(clientInfo)->setLoadProgress(100);
183 }
184
185 void QtWebPageLoadClient::didFirstVisuallyNonEmptyLayoutForFrame(WKPageRef, WKFrameRef frame, WKTypeRef, const void *clientInfo)
186 {
187     if (!WKFrameIsMainFrame(frame))
188         return;
189     toQtWebPageLoadClient(clientInfo)->didFirstVisuallyNonEmptyLayoutForFrame();
190 }
191
192 void QtWebPageLoadClient::didChangeBackForwardList(WKPageRef, WKBackForwardListItemRef, WKArrayRef, const void *clientInfo)
193 {
194     toQtWebPageLoadClient(clientInfo)->didChangeBackForwardList();
195 }