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 "WebKitScriptDialog.h"
22
23#include "WebKitScriptDialogPrivate.h"
24
25G_DEFINE_BOXED_TYPE(WebKitScriptDialog, webkit_script_dialog, webkit_script_dialog_ref, webkit_script_dialog_unref)
26
27WebKitScriptDialog* webkitScriptDialogCreate(unsigned type, const CString& message, const CString& defaultText, Function<void(bool, const String&)>&& completionHandler)
28{
29 auto* dialog = static_cast<WebKitScriptDialog*>(fastMalloc(sizeof(WebKitScriptDialog)));
30 new (dialog) WebKitScriptDialog(type, message, defaultText, WTFMove(completionHandler));
31 return dialog;
32}
33
34bool webkitScriptDialogIsRunning(WebKitScriptDialog* scriptDialog)
35{
36 return !!scriptDialog->completionHandler;
37}
38
39/**
40 * webkit_script_dialog_ref:
41 * @dialog: a #WebKitScriptDialog
42 *
43 * Atomically increments the reference count of @dialog by one. This
44 * function is MT-safe and may be called from any thread.
45 *
46 * Returns: The passed in #WebKitScriptDialog
47 *
48 * Since: 2.24
49 */
50WebKitScriptDialog* webkit_script_dialog_ref(WebKitScriptDialog* dialog)
51{
52 g_atomic_int_inc(&dialog->referenceCount);
53 return dialog;
54}
55
56/**
57 * webkit_script_dialog_unref:
58 * @dialog: a #WebKitScriptDialog
59 *
60 * Atomically decrements the reference count of @dialog by one. If the
61 * reference count drops to 0, all memory allocated by the #WebKitScriptdialog is
62 * released. This function is MT-safe and may be called from any
63 * thread.
64 *
65 * Since: 2.24
66 */
67void webkit_script_dialog_unref(WebKitScriptDialog* dialog)
68{
69 if (g_atomic_int_dec_and_test(&dialog->referenceCount)) {
70 webkit_script_dialog_close(dialog);
71 dialog->~WebKitScriptDialog();
72 fastFree(dialog);
73 }
74}
75
76/**
77 * webkit_script_dialog_get_dialog_type:
78 * @dialog: a #WebKitScriptDialog
79 *
80 * Get the dialog type of a #WebKitScriptDialog.
81 *
82 * Returns: the #WebKitScriptDialogType of @dialog
83 */
84WebKitScriptDialogType webkit_script_dialog_get_dialog_type(WebKitScriptDialog* dialog)
85{
86 g_return_val_if_fail(dialog, WEBKIT_SCRIPT_DIALOG_ALERT);
87
88 return static_cast<WebKitScriptDialogType>(dialog->type);
89}
90
91/**
92 * webkit_script_dialog_get_message:
93 * @dialog: a #WebKitScriptDialog
94 *
95 * Get the message of a #WebKitScriptDialog.
96 *
97 * Returns: the message of @dialog.
98 */
99const char* webkit_script_dialog_get_message(WebKitScriptDialog* dialog)
100{
101 g_return_val_if_fail(dialog, 0);
102
103 return dialog->message.data();
104}
105
106/**
107 * webkit_script_dialog_confirm_set_confirmed:
108 * @dialog: a #WebKitScriptDialog
109 * @confirmed: whether user confirmed the dialog
110 *
111 * This method is used for %WEBKIT_SCRIPT_DIALOG_CONFIRM and %WEBKIT_SCRIPT_DIALOG_BEFORE_UNLOAD_CONFIRM dialogs when
112 * #WebKitWebView::script-dialog signal is emitted to set whether the user
113 * confirmed the dialog or not. The default implementation of #WebKitWebView::script-dialog
114 * signal sets %TRUE when the OK or Stay buttons are clicked and %FALSE otherwise.
115 * It's an error to use this method with a #WebKitScriptDialog that is not of type
116 * %WEBKIT_SCRIPT_DIALOG_CONFIRM or %WEBKIT_SCRIPT_DIALOG_BEFORE_UNLOAD_CONFIRM
117 */
118void webkit_script_dialog_confirm_set_confirmed(WebKitScriptDialog* dialog, gboolean confirmed)
119{
120 g_return_if_fail(dialog);
121 g_return_if_fail(dialog->type == WEBKIT_SCRIPT_DIALOG_CONFIRM || dialog->type == WEBKIT_SCRIPT_DIALOG_BEFORE_UNLOAD_CONFIRM);
122
123 dialog->confirmed = confirmed;
124}
125
126/**
127 * webkit_script_dialog_prompt_get_default_text:
128 * @dialog: a #WebKitScriptDialog
129 *
130 * Get the default text of a #WebKitScriptDialog of type %WEBKIT_SCRIPT_DIALOG_PROMPT.
131 * It's an error to use this method with a #WebKitScriptDialog that is not of type
132 * %WEBKIT_SCRIPT_DIALOG_PROMPT.
133 *
134 * Returns: the default text of @dialog
135 */
136const char* webkit_script_dialog_prompt_get_default_text(WebKitScriptDialog* dialog)
137{
138 g_return_val_if_fail(dialog, 0);
139 g_return_val_if_fail(dialog->type == WEBKIT_SCRIPT_DIALOG_PROMPT, 0);
140
141 return dialog->defaultText.data();
142}
143
144/**
145 * webkit_script_dialog_prompt_set_text:
146 * @dialog: a #WebKitScriptDialog
147 * @text: the text to set
148 *
149 * This method is used for %WEBKIT_SCRIPT_DIALOG_PROMPT dialogs when
150 * #WebKitWebView::script-dialog signal is emitted to set the text
151 * entered by the user. The default implementation of #WebKitWebView::script-dialog
152 * signal sets the text of the entry form when OK button is clicked, otherwise %NULL is set.
153 * It's an error to use this method with a #WebKitScriptDialog that is not of type
154 * %WEBKIT_SCRIPT_DIALOG_PROMPT.
155 */
156void webkit_script_dialog_prompt_set_text(WebKitScriptDialog* dialog, const char* text)
157{
158 g_return_if_fail(dialog);
159 g_return_if_fail(dialog->type == WEBKIT_SCRIPT_DIALOG_PROMPT);
160
161 dialog->text = text;
162}
163
164/**
165 * webkit_script_dialog_close:
166 * @dialog: a #WebKitScriptDialog
167 *
168 * Close @dialog. When handling a #WebKitScriptDialog asynchronously (webkit_script_dialog_ref()
169 * was called in #WebKitWebView::script-dialog callback), this function needs to be called to notify
170 * that we are done with the script dialog. The dialog will be closed on destruction if this function
171 * hasn't been called before.
172 *
173 * Since: 2.24
174 */
175void webkit_script_dialog_close(WebKitScriptDialog* dialog)
176{
177 g_return_if_fail(dialog);
178
179 if (!dialog->completionHandler)
180 return;
181
182 auto completionHandler = std::exchange(dialog->completionHandler, nullptr);
183
184 switch (dialog->type) {
185 case WEBKIT_SCRIPT_DIALOG_ALERT:
186 completionHandler(false, emptyString());
187 break;
188 case WEBKIT_SCRIPT_DIALOG_CONFIRM:
189 case WEBKIT_SCRIPT_DIALOG_BEFORE_UNLOAD_CONFIRM:
190 completionHandler(dialog->confirmed, emptyString());
191 break;
192 case WEBKIT_SCRIPT_DIALOG_PROMPT:
193 completionHandler(false, String::fromUTF8(dialog->text.data()));
194 break;
195 }
196}
197