1/*
2 * Copyright (C) 2012 Igalia S.L.
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 library 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 library; 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#include "config.h"
21#include "WebKitResponsePolicyDecision.h"
22
23#include "WebKitPolicyDecisionPrivate.h"
24#include "WebKitResponsePolicyDecisionPrivate.h"
25#include "WebKitURIRequestPrivate.h"
26#include "WebKitURIResponsePrivate.h"
27#include <glib/gi18n-lib.h>
28#include <wtf/glib/GRefPtr.h>
29#include <wtf/glib/WTFGType.h>
30#include <wtf/text/CString.h>
31
32using namespace WebKit;
33using namespace WebCore;
34
35/**
36 * SECTION: WebKitResponsePolicyDecision
37 * @Short_description: A policy decision for resource responses
38 * @Title: WebKitResponsePolicyDecision
39 * @See_also: #WebKitPolicyDecision, #WebKitWebView
40 *
41 * WebKitResponsePolicyDecision represents a policy decision for a
42 * resource response, whether from the network or the local system.
43 * A very common use case for these types of decision is deciding
44 * whether or not to download a particular resource or to load it
45 * normally.
46 */
47
48struct _WebKitResponsePolicyDecisionPrivate {
49 RefPtr<API::NavigationResponse> navigationResponse;
50 GRefPtr<WebKitURIRequest> request;
51 GRefPtr<WebKitURIResponse> response;
52};
53
54WEBKIT_DEFINE_TYPE(WebKitResponsePolicyDecision, webkit_response_policy_decision, WEBKIT_TYPE_POLICY_DECISION)
55
56enum {
57 PROP_0,
58 PROP_REQUEST,
59 PROP_RESPONSE,
60};
61
62static void webkitResponsePolicyDecisionGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec)
63{
64 WebKitResponsePolicyDecision* decision = WEBKIT_RESPONSE_POLICY_DECISION(object);
65 switch (propId) {
66 case PROP_REQUEST:
67 g_value_set_object(value, webkit_response_policy_decision_get_request(decision));
68 break;
69 case PROP_RESPONSE:
70 g_value_set_object(value, webkit_response_policy_decision_get_response(decision));
71 break;
72 default:
73 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
74 break;
75 }
76}
77
78static void webkit_response_policy_decision_class_init(WebKitResponsePolicyDecisionClass* decisionClass)
79{
80 GObjectClass* objectClass = G_OBJECT_CLASS(decisionClass);
81 objectClass->get_property = webkitResponsePolicyDecisionGetProperty;
82
83 /**
84 * WebKitResponsePolicyDecision:request:
85 *
86 * This property contains the #WebKitURIRequest associated with this
87 * policy decision.
88 */
89 g_object_class_install_property(objectClass,
90 PROP_REQUEST,
91 g_param_spec_object("request",
92 _("Response URI request"),
93 _("The URI request that is associated with this policy decision"),
94 WEBKIT_TYPE_URI_REQUEST,
95 WEBKIT_PARAM_READABLE));
96
97 /**
98 * WebKitResponsePolicyDecision:response:
99 *
100 * This property contains the #WebKitURIResponse associated with this
101 * policy decision.
102 */
103 g_object_class_install_property(objectClass,
104 PROP_RESPONSE,
105 g_param_spec_object("response",
106 _("URI response"),
107 _("The URI response that is associated with this policy decision"),
108 WEBKIT_TYPE_URI_RESPONSE,
109 WEBKIT_PARAM_READABLE));
110
111}
112
113/**
114 * webkit_response_policy_decision_get_request:
115 * @decision: a #WebKitResponsePolicyDecision
116 *
117 * Return the #WebKitURIRequest associated with the response decision.
118 * Modifications to the returned object are <emphasis>not</emphasis> taken
119 * into account when the request is sent over the network, and is intended
120 * only to aid in evaluating whether a response decision should be taken or
121 * not. To modify requests before they are sent over the network the
122 * #WebKitPage::send-request signal can be used instead.
123 *
124 * Returns: (transfer none): The URI request that is associated with this policy decision.
125 */
126WebKitURIRequest* webkit_response_policy_decision_get_request(WebKitResponsePolicyDecision* decision)
127{
128 g_return_val_if_fail(WEBKIT_IS_RESPONSE_POLICY_DECISION(decision), nullptr);
129 if (!decision->priv->request)
130 decision->priv->request = adoptGRef(webkitURIRequestCreateForResourceRequest(decision->priv->navigationResponse->request()));
131 return decision->priv->request.get();
132}
133
134/**
135 * webkit_response_policy_decision_get_response:
136 * @decision: a #WebKitResponsePolicyDecision
137 *
138 * Gets the value of the #WebKitResponsePolicyDecision:response property.
139 *
140 * Returns: (transfer none): The URI response that is associated with this policy decision.
141 */
142WebKitURIResponse* webkit_response_policy_decision_get_response(WebKitResponsePolicyDecision* decision)
143{
144 g_return_val_if_fail(WEBKIT_IS_RESPONSE_POLICY_DECISION(decision), nullptr);
145 if (!decision->priv->response)
146 decision->priv->response = adoptGRef(webkitURIResponseCreateForResourceResponse(decision->priv->navigationResponse->response()));
147 return decision->priv->response.get();
148}
149
150/**
151 * webkit_response_policy_decision_is_mime_type_supported:
152 * @decision: a #WebKitResponsePolicyDecision
153 *
154 * Gets whether the MIME type of the response can be displayed in the #WebKitWebView
155 * that triggered this policy decision request. See also webkit_web_view_can_show_mime_type().
156 *
157 * Returns: %TRUE if the MIME type of the response is supported or %FALSE otherwise
158 *
159 * Since: 2.4
160 */
161gboolean webkit_response_policy_decision_is_mime_type_supported(WebKitResponsePolicyDecision* decision)
162{
163 g_return_val_if_fail(WEBKIT_IS_RESPONSE_POLICY_DECISION(decision), FALSE);
164 return decision->priv->navigationResponse->canShowMIMEType();
165}
166
167WebKitPolicyDecision* webkitResponsePolicyDecisionCreate(Ref<API::NavigationResponse>&& response, Ref<WebKit::WebFramePolicyListenerProxy>&& listener)
168{
169 WebKitResponsePolicyDecision* responseDecision = WEBKIT_RESPONSE_POLICY_DECISION(g_object_new(WEBKIT_TYPE_RESPONSE_POLICY_DECISION, nullptr));
170 responseDecision->priv->navigationResponse = WTFMove(response);
171 WebKitPolicyDecision* decision = WEBKIT_POLICY_DECISION(responseDecision);
172 webkitPolicyDecisionSetListener(decision, WTFMove(listener));
173 return decision;
174}
175