1/*
2 * Copyright (C) 2012 Intel Corporation. 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 "PageLoadClientEfl.h"
28
29#include "EwkView.h"
30#include "PageViewportController.h"
31#include "WKAPICast.h"
32#include "WKFrame.h"
33#include "WKPage.h"
34#include "ewk_auth_request_private.h"
35#include "ewk_back_forward_list_private.h"
36#include "ewk_error_private.h"
37#include "ewk_view.h"
38
39using namespace EwkViewCallbacks;
40
41namespace WebKit {
42
43static inline PageLoadClientEfl* toPageLoadClientEfl(const void* clientInfo)
44{
45    return static_cast<PageLoadClientEfl*>(const_cast<void*>(clientInfo));
46}
47
48void PageLoadClientEfl::didReceiveTitleForFrame(WKPageRef, WKStringRef title, WKFrameRef frame, WKTypeRef, const void* clientInfo)
49{
50    if (!WKFrameIsMainFrame(frame))
51        return;
52
53    EwkView* view = toPageLoadClientEfl(clientInfo)->view();
54    view->smartCallback<TitleChange>().call(toImpl(title)->string());
55}
56
57void PageLoadClientEfl::didChangeProgress(WKPageRef page, const void* clientInfo)
58{
59    EwkView* view = toPageLoadClientEfl(clientInfo)->view();
60    double progress = WKPageGetEstimatedProgress(page);
61    view->smartCallback<LoadProgress>().call(&progress);
62}
63
64void PageLoadClientEfl::didFinishLoadForFrame(WKPageRef, WKFrameRef frame, WKTypeRef /*userData*/, const void* clientInfo)
65{
66    if (!WKFrameIsMainFrame(frame))
67        return;
68
69    EwkView* view = toPageLoadClientEfl(clientInfo)->view();
70    view->smartCallback<LoadFinished>().call();
71}
72
73void PageLoadClientEfl::didFailLoadWithErrorForFrame(WKPageRef, WKFrameRef frame, WKErrorRef error, WKTypeRef, const void* clientInfo)
74{
75    if (!WKFrameIsMainFrame(frame))
76        return;
77
78    EwkView* view = toPageLoadClientEfl(clientInfo)->view();
79    auto ewkError = std::make_unique<EwkError>(error);
80    view->smartCallback<LoadError>().call(ewkError.get());
81    view->smartCallback<LoadFinished>().call();
82}
83
84void PageLoadClientEfl::didStartProvisionalLoadForFrame(WKPageRef, WKFrameRef frame, WKTypeRef /*userData*/, const void* clientInfo)
85{
86    if (!WKFrameIsMainFrame(frame))
87        return;
88
89    EwkView* view = toPageLoadClientEfl(clientInfo)->view();
90    view->informURLChange();
91    view->smartCallback<ProvisionalLoadStarted>().call();
92}
93
94void PageLoadClientEfl::didReceiveServerRedirectForProvisionalLoadForFrame(WKPageRef, WKFrameRef frame, WKTypeRef /*userData*/, const void* clientInfo)
95{
96    if (!WKFrameIsMainFrame(frame))
97        return;
98
99    EwkView* view = toPageLoadClientEfl(clientInfo)->view();
100    view->informURLChange();
101    view->smartCallback<ProvisionalLoadRedirect>().call();
102}
103
104void PageLoadClientEfl::didFailProvisionalLoadWithErrorForFrame(WKPageRef, WKFrameRef frame, WKErrorRef error, WKTypeRef, const void* clientInfo)
105{
106    if (!WKFrameIsMainFrame(frame))
107        return;
108
109    EwkView* view = toPageLoadClientEfl(clientInfo)->view();
110    auto ewkError = std::make_unique<EwkError>(error);
111    view->smartCallback<ProvisionalLoadFailed>().call(ewkError.get());
112}
113
114void PageLoadClientEfl::didCommitLoadForFrame(WKPageRef, WKFrameRef frame, WKTypeRef, const void* clientInfo)
115{
116    if (!WKFrameIsMainFrame(frame))
117        return;
118
119    EwkView* view = toPageLoadClientEfl(clientInfo)->view();
120    if (WKPageUseFixedLayout(view->wkPage()))
121        view->pageViewportController().didCommitLoad();
122    else
123        view->scheduleUpdateDisplay();
124
125    view->setWaitingForNewPage();
126}
127
128void PageLoadClientEfl::didChangeBackForwardList(WKPageRef, WKBackForwardListItemRef addedItem, WKArrayRef removedItems, const void* clientInfo)
129{
130    EwkView* view = toPageLoadClientEfl(clientInfo)->view();
131    ASSERT(view);
132
133    Ewk_Back_Forward_List* list = ewk_view_back_forward_list_get(view->evasObject());
134    ASSERT(list);
135    list->update(addedItem, removedItems);
136
137    view->smartCallback<BackForwardListChange>().call();
138}
139
140void PageLoadClientEfl::didSameDocumentNavigationForFrame(WKPageRef, WKFrameRef frame, WKSameDocumentNavigationType, WKTypeRef, const void* clientInfo)
141{
142    if (!WKFrameIsMainFrame(frame))
143        return;
144
145    EwkView* view = toPageLoadClientEfl(clientInfo)->view();
146    view->informURLChange();
147}
148
149void PageLoadClientEfl::didReceiveAuthenticationChallengeInFrame(WKPageRef, WKFrameRef, WKAuthenticationChallengeRef authenticationChallenge, const void* clientInfo)
150{
151    EwkView* view = toPageLoadClientEfl(clientInfo)->view();
152
153    RefPtr<EwkAuthRequest> authenticationRequest = EwkAuthRequest::create(authenticationChallenge);
154    view->smartCallback<AuthenticationRequest>().call(authenticationRequest.get());
155}
156
157PageLoadClientEfl::PageLoadClientEfl(EwkView* view)
158    : m_view(view)
159{
160    WKPageRef pageRef = m_view->wkPage();
161    ASSERT(pageRef);
162
163    WKPageLoaderClientV3 loadClient;
164    memset(&loadClient, 0, sizeof(WKPageLoaderClient));
165    loadClient.base.version = 3;
166    loadClient.base.clientInfo = this;
167    loadClient.didReceiveTitleForFrame = didReceiveTitleForFrame;
168    loadClient.didStartProgress = didChangeProgress;
169    loadClient.didChangeProgress = didChangeProgress;
170    loadClient.didFinishProgress = didChangeProgress;
171    loadClient.didFinishLoadForFrame = didFinishLoadForFrame;
172    loadClient.didFailLoadWithErrorForFrame = didFailLoadWithErrorForFrame;
173    loadClient.didStartProvisionalLoadForFrame = didStartProvisionalLoadForFrame;
174    loadClient.didReceiveServerRedirectForProvisionalLoadForFrame = didReceiveServerRedirectForProvisionalLoadForFrame;
175    loadClient.didFailProvisionalLoadWithErrorForFrame = didFailProvisionalLoadWithErrorForFrame;
176    loadClient.didCommitLoadForFrame = didCommitLoadForFrame;
177    loadClient.didChangeBackForwardList = didChangeBackForwardList;
178    loadClient.didSameDocumentNavigationForFrame = didSameDocumentNavigationForFrame;
179    loadClient.didReceiveAuthenticationChallengeInFrame = didReceiveAuthenticationChallengeInFrame;
180    WKPageSetPageLoaderClient(pageRef, &loadClient.base);
181}
182
183} // namespace WebKit
184