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 "PagePolicyClientEfl.h"
28
29#include "EwkView.h"
30#include "WKFrame.h"
31#include "WKFramePolicyListener.h"
32#include "WKString.h"
33#include "WKURLResponse.h"
34#include "ewk_navigation_policy_decision.h"
35#include "ewk_navigation_policy_decision_private.h"
36#include <WebCore/HTTPStatusCodes.h>
37#include <wtf/text/CString.h>
38
39using namespace EwkViewCallbacks;
40
41namespace WebKit {
42
43static inline PagePolicyClientEfl* toPagePolicyClientEfl(const void* clientInfo)
44{
45    return static_cast<PagePolicyClientEfl*>(const_cast<void*>(clientInfo));
46}
47
48void PagePolicyClientEfl::decidePolicyForNavigationAction(WKPageRef, WKFrameRef, WKFrameNavigationType navigationType, WKEventModifiers modifiers, WKEventMouseButton mouseButton, WKFrameRef, WKURLRequestRef request, WKFramePolicyListenerRef listener, WKTypeRef /*userData*/, const void* clientInfo)
49{
50    PagePolicyClientEfl* policyClient = toPagePolicyClientEfl(clientInfo);
51
52    RefPtr<EwkNavigationPolicyDecision> decision = EwkNavigationPolicyDecision::create(navigationType, mouseButton, modifiers, request, 0, listener);
53    policyClient->m_view->smartCallback<NavigationPolicyDecision>().call(decision.get());
54}
55
56void PagePolicyClientEfl::decidePolicyForNewWindowAction(WKPageRef, WKFrameRef, WKFrameNavigationType navigationType, WKEventModifiers modifiers, WKEventMouseButton mouseButton, WKURLRequestRef request, WKStringRef frameName, WKFramePolicyListenerRef listener, WKTypeRef /*userData*/, const void* clientInfo)
57{
58    PagePolicyClientEfl* policyClient = toPagePolicyClientEfl(clientInfo);
59
60    RefPtr<EwkNavigationPolicyDecision> decision = EwkNavigationPolicyDecision::create(navigationType, mouseButton, modifiers, request, toImpl(frameName)->string().utf8().data(), listener);
61    policyClient->m_view->smartCallback<NewWindowPolicyDecision>().call(decision.get());
62}
63
64void PagePolicyClientEfl::decidePolicyForResponseCallback(WKPageRef, WKFrameRef frame, WKURLResponseRef response, WKURLRequestRef, bool canShowMIMEType, WKFramePolicyListenerRef listener, WKTypeRef /*userData*/, const void* /*clientInfo*/)
65{
66    // Ignore responses with an HTTP status code of 204 (No Content)
67    if (WKURLResponseHTTPStatusCode(response) == WebCore::HTTPNoContent) {
68        WKFramePolicyListenerIgnore(listener);
69        return;
70    }
71
72    // If the URL Response has "Content-Disposition: attachment;" header, then
73    // we should download it.
74    if (WKURLResponseIsAttachment(response)) {
75        WKFramePolicyListenerDownload(listener);
76        return;
77    }
78
79    WKRetainPtr<WKStringRef> mimeType = adoptWK(WKURLResponseCopyMIMEType(response));
80    if (WKFrameIsMainFrame(frame)) {
81        if (canShowMIMEType) {
82            WKFramePolicyListenerUse(listener);
83            return;
84        }
85
86        // If we can't use (show) it then we should download it.
87        WKFramePolicyListenerDownload(listener);
88        return;
89    }
90
91    // We should ignore downloadable top-level content for subframes, with an exception for text/xml and application/xml so we can still support Acid3 test.
92    // It makes the browser intentionally behave differently when it comes to text(application)/xml content in subframes vs. mainframe.
93    bool isXMLType = WKStringIsEqualToUTF8CString(mimeType.get(), "text/xml") || WKStringIsEqualToUTF8CString(mimeType.get(), "application/xml");
94    if (!canShowMIMEType && !isXMLType) {
95        WKFramePolicyListenerIgnore(listener);
96        return;
97    }
98
99    WKFramePolicyListenerUse(listener);
100}
101
102PagePolicyClientEfl::PagePolicyClientEfl(EwkView* view)
103    : m_view(view)
104{
105    WKPageRef pageRef = m_view->wkPage();
106    ASSERT(pageRef);
107
108    WKPagePolicyClientV1 policyClient;
109    memset(&policyClient, 0, sizeof(WKPagePolicyClient));
110    policyClient.base.version = 1;
111    policyClient.base.clientInfo = this;
112    policyClient.decidePolicyForNavigationAction = decidePolicyForNavigationAction;
113    policyClient.decidePolicyForNewWindowAction = decidePolicyForNewWindowAction;
114    policyClient.decidePolicyForResponse = decidePolicyForResponseCallback;
115
116    WKPageSetPagePolicyClient(pageRef, &policyClient.base);
117}
118
119} // namespace WebKit
120