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 "WebKitPolicyDecision.h"
22
23#include "WebFramePolicyListenerProxy.h"
24#include "WebKitPolicyDecisionPrivate.h"
25#include "WebsitePoliciesData.h"
26#include <wtf/glib/WTFGType.h>
27
28using namespace WebKit;
29
30/**
31 * SECTION: WebKitPolicyDecision
32 * @Short_description: A pending policy decision
33 * @Title: WebKitPolicyDecision
34 * @See_also: #WebKitWebView
35 *
36 * Often WebKit allows the client to decide the policy for certain
37 * operations. For instance, a client may want to open a link in a new
38 * tab, block a navigation entirely, query the user or trigger a download
39 * instead of a navigation. In these cases WebKit will fire the
40 * #WebKitWebView::decide-policy signal with a #WebKitPolicyDecision
41 * object. If the signal handler does nothing, WebKit will act as if
42 * webkit_policy_decision_use() was called as soon as signal handling
43 * completes. To make a policy decision asynchronously, simply increment
44 * the reference count of the #WebKitPolicyDecision object.
45 */
46
47struct _WebKitPolicyDecisionPrivate {
48 RefPtr<WebFramePolicyListenerProxy> listener;
49};
50
51WEBKIT_DEFINE_ABSTRACT_TYPE(WebKitPolicyDecision, webkit_policy_decision, G_TYPE_OBJECT)
52
53static void webkitPolicyDecisionDispose(GObject* object)
54{
55 webkit_policy_decision_use(WEBKIT_POLICY_DECISION(object));
56 G_OBJECT_CLASS(webkit_policy_decision_parent_class)->dispose(object);
57}
58
59void webkitPolicyDecisionSetListener(WebKitPolicyDecision* decision, Ref<WebFramePolicyListenerProxy>&& listener)
60{
61 decision->priv->listener = WTFMove(listener);
62}
63
64static void webkit_policy_decision_class_init(WebKitPolicyDecisionClass* decisionClass)
65{
66 GObjectClass* objectClass = G_OBJECT_CLASS(decisionClass);
67 objectClass->dispose = webkitPolicyDecisionDispose;
68}
69
70/**
71 * webkit_policy_decision_use:
72 * @decision: a #WebKitPolicyDecision
73 *
74 * Accept the action which triggered this decision.
75 */
76void webkit_policy_decision_use(WebKitPolicyDecision* decision)
77{
78 g_return_if_fail(WEBKIT_IS_POLICY_DECISION(decision));
79
80 if (!decision->priv->listener)
81 return;
82
83 auto listener = std::exchange(decision->priv->listener, nullptr);
84 listener->use();
85}
86
87/**
88 * webkit_policy_decision_ignore:
89 * @decision: a #WebKitPolicyDecision
90 *
91 * Ignore the action which triggered this decision. For instance, for a
92 * #WebKitResponsePolicyDecision, this would cancel the request.
93 */
94void webkit_policy_decision_ignore(WebKitPolicyDecision* decision)
95{
96 g_return_if_fail(WEBKIT_IS_POLICY_DECISION(decision));
97
98 if (!decision->priv->listener)
99 return;
100
101 auto listener = std::exchange(decision->priv->listener, nullptr);
102 listener->ignore();
103}
104
105/**
106 * webkit_policy_decision_download:
107 * @decision: a #WebKitPolicyDecision
108 *
109 * Spawn a download from this decision.
110 */
111void webkit_policy_decision_download(WebKitPolicyDecision* decision)
112{
113 g_return_if_fail(WEBKIT_IS_POLICY_DECISION(decision));
114
115 if (!decision->priv->listener)
116 return;
117
118 auto listener = std::exchange(decision->priv->listener, nullptr);
119 listener->download();
120}
121