9476255
[WebKit.git] /
1 /*
2  * Copyright (C) 2011 Igalia S.L.
3  * Portions Copyright (c) 2011 Motorola Mobility, Inc.  All rights reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22 #include "WebKitLoaderClient.h"
23
24 #include "WebKitBackForwardListPrivate.h"
25 #include "WebKitWebViewBasePrivate.h"
26 #include "WebKitWebViewPrivate.h"
27 #include <wtf/gobject/GOwnPtr.h>
28 #include <wtf/text/CString.h>
29
30 using namespace WebKit;
31 using namespace WebCore;
32
33 static void didStartProvisionalLoadForFrame(WKPageRef page, WKFrameRef frame, WKTypeRef userData, const void* clientInfo)
34 {
35     if (!WKFrameIsMainFrame(frame))
36         return;
37
38     webkitWebViewLoadChanged(WEBKIT_WEB_VIEW(clientInfo), WEBKIT_LOAD_STARTED);
39 }
40
41 static void didReceiveServerRedirectForProvisionalLoadForFrame(WKPageRef page, WKFrameRef frame, WKTypeRef userData, const void* clientInfo)
42 {
43     if (!WKFrameIsMainFrame(frame))
44         return;
45
46     webkitWebViewLoadChanged(WEBKIT_WEB_VIEW(clientInfo), WEBKIT_LOAD_REDIRECTED);
47 }
48
49 static void didFailProvisionalLoadWithErrorForFrame(WKPageRef page, WKFrameRef frame, WKErrorRef error, WKTypeRef userData, const void* clientInfo)
50 {
51     if (!WKFrameIsMainFrame(frame))
52         return;
53
54     const ResourceError& resourceError = toImpl(error)->platformError();
55     GOwnPtr<GError> webError(g_error_new_literal(g_quark_from_string(resourceError.domain().utf8().data()),
56                                                  resourceError.errorCode(),
57                                                  resourceError.localizedDescription().utf8().data()));
58     webkitWebViewLoadFailed(WEBKIT_WEB_VIEW(clientInfo), WEBKIT_LOAD_STARTED,
59                             resourceError.failingURL().utf8().data(), webError.get());
60 }
61
62 static void didCommitLoadForFrame(WKPageRef page, WKFrameRef frame, WKTypeRef userData, const void* clientInfo)
63 {
64     if (!WKFrameIsMainFrame(frame))
65         return;
66
67     webkitWebViewLoadChanged(WEBKIT_WEB_VIEW(clientInfo), WEBKIT_LOAD_COMMITTED);
68 }
69
70 static void didFinishLoadForFrame(WKPageRef page, WKFrameRef frame, WKTypeRef userData, const void* clientInfo)
71 {
72     if (!WKFrameIsMainFrame(frame))
73         return;
74
75     webkitWebViewLoadChanged(WEBKIT_WEB_VIEW(clientInfo), WEBKIT_LOAD_FINISHED);
76 }
77
78 static void didFailLoadWithErrorForFrame(WKPageRef page, WKFrameRef frame, WKErrorRef error, WKTypeRef, const void* clientInfo)
79 {
80     if (!WKFrameIsMainFrame(frame))
81         return;
82
83     const ResourceError& resourceError = toImpl(error)->platformError();
84     GOwnPtr<GError> webError(g_error_new_literal(g_quark_from_string(resourceError.domain().utf8().data()),
85                                                  resourceError.errorCode(),
86                                                  resourceError.localizedDescription().utf8().data()));
87     webkitWebViewLoadFailed(WEBKIT_WEB_VIEW(clientInfo), WEBKIT_LOAD_COMMITTED,
88                             resourceError.failingURL().utf8().data(), webError.get());
89 }
90
91 static void didSameDocumentNavigationForFrame(WKPageRef page, WKFrameRef frame, WKSameDocumentNavigationType, WKTypeRef, const void* clientInfo)
92 {
93     if (!WKFrameIsMainFrame(frame))
94         return;
95
96     webkitWebViewUpdateURI(WEBKIT_WEB_VIEW(clientInfo));
97 }
98
99 static void didReceiveTitleForFrame(WKPageRef page, WKStringRef titleRef, WKFrameRef frameRef, WKTypeRef, const void* clientInfo)
100 {
101     if (!WKFrameIsMainFrame(frameRef))
102         return;
103
104     webkitWebViewSetTitle(WEBKIT_WEB_VIEW(clientInfo), toImpl(titleRef)->string().utf8());
105 }
106
107 static void didChangeProgress(WKPageRef page, const void* clientInfo)
108 {
109     webkitWebViewSetEstimatedLoadProgress(WEBKIT_WEB_VIEW(clientInfo), WKPageGetEstimatedProgress(page));
110 }
111
112 static void didChangeBackForwardList(WKPageRef page, WKBackForwardListItemRef addedItem, WKArrayRef removedItems, const void* clientInfo)
113 {
114     webkitBackForwardListChanged(webkit_web_view_get_back_forward_list(WEBKIT_WEB_VIEW(clientInfo)), addedItem, removedItems);
115 }
116
117 void attachLoaderClientToView(WebKitWebView* webView)
118 {
119     WKPageLoaderClient wkLoaderClient = {
120         kWKPageLoaderClientCurrentVersion,
121         webView, // clientInfo
122         didStartProvisionalLoadForFrame,
123         didReceiveServerRedirectForProvisionalLoadForFrame,
124         didFailProvisionalLoadWithErrorForFrame,
125         didCommitLoadForFrame,
126         0, // didFinishDocumentLoadForFrame
127         didFinishLoadForFrame,
128         didFailLoadWithErrorForFrame,
129         didSameDocumentNavigationForFrame,
130         didReceiveTitleForFrame,
131         0, // didFirstLayoutForFrame
132         0, // didFirstVisuallyNonEmptyLayoutForFrame
133         0, // didRemoveFrameFromHierarchy
134         0, // didDisplayInsecureContentForFrame
135         0, // didRunInsecureContentForFrame
136         0, // canAuthenticateAgainstProtectionSpaceInFrame
137         0, // didReceiveAuthenticationChallengeInFrame
138         didChangeProgress, // didStartProgress
139         didChangeProgress,
140         didChangeProgress, // didFinishProgress
141         0, // didBecomeUnresponsive
142         0, // didBecomeResponsive
143         0, // processDidCrash
144         didChangeBackForwardList,
145         0, // shouldGoToBackForwardListItem
146         0, // didFailToInitializePlugin
147         0, // didDetectXSSForFrame
148         0, // didFirstVisuallyNonEmptyLayoutForFrame
149         0, // willGoToBackForwardListItem
150         0 // interactionOccurredWhileProcessUnresponsive
151     };
152     WKPageRef wkPage = toAPI(webkitWebViewBaseGetPage(WEBKIT_WEB_VIEW_BASE(webView)));
153     WKPageSetPageLoaderClient(wkPage, &wkLoaderClient);
154 }
155