| 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 "WebKitFormSubmissionRequest.h" |
| 22 | |
| 23 | #include "APIDictionary.h" |
| 24 | #include "APIString.h" |
| 25 | #include "WebFormSubmissionListenerProxy.h" |
| 26 | #include "WebKitFormSubmissionRequestPrivate.h" |
| 27 | #include <wtf/glib/GRefPtr.h> |
| 28 | #include <wtf/glib/GUniquePtr.h> |
| 29 | #include <wtf/glib/WTFGType.h> |
| 30 | #include <wtf/text/CString.h> |
| 31 | |
| 32 | using namespace WebKit; |
| 33 | |
| 34 | /** |
| 35 | * SECTION: WebKitFormSubmissionRequest |
| 36 | * @Short_description: Represents a form submission request |
| 37 | * @Title: WebKitFormSubmissionRequest |
| 38 | * |
| 39 | * When a form is about to be submitted in a #WebKitWebView, the |
| 40 | * #WebKitWebView::submit-form signal is emitted. Its request argument |
| 41 | * contains information about the text fields of the form, that are |
| 42 | * typically used to store login information, returned as lists by |
| 43 | * webkit_form_submission_request_list_text_fields(). You can submit the |
| 44 | * form with webkit_form_submission_request_submit(). |
| 45 | */ |
| 46 | |
| 47 | struct _WebKitFormSubmissionRequestPrivate { |
| 48 | RefPtr<WebFormSubmissionListenerProxy> listener; |
| 49 | GRefPtr<GPtrArray> textFieldNames; |
| 50 | GRefPtr<GPtrArray> textFieldValues; |
| 51 | GRefPtr<GHashTable> values; |
| 52 | bool handledRequest; |
| 53 | }; |
| 54 | |
| 55 | WEBKIT_DEFINE_TYPE(WebKitFormSubmissionRequest, webkit_form_submission_request, G_TYPE_OBJECT) |
| 56 | |
| 57 | static void webkitFormSubmissionRequestDispose(GObject* object) |
| 58 | { |
| 59 | WebKitFormSubmissionRequest* request = WEBKIT_FORM_SUBMISSION_REQUEST(object); |
| 60 | |
| 61 | // Make sure the request is always handled before finalizing. |
| 62 | if (!request->priv->handledRequest) |
| 63 | webkit_form_submission_request_submit(request); |
| 64 | |
| 65 | G_OBJECT_CLASS(webkit_form_submission_request_parent_class)->dispose(object); |
| 66 | } |
| 67 | |
| 68 | static void webkit_form_submission_request_class_init(WebKitFormSubmissionRequestClass* requestClass) |
| 69 | { |
| 70 | GObjectClass* objectClass = G_OBJECT_CLASS(requestClass); |
| 71 | objectClass->dispose = webkitFormSubmissionRequestDispose; |
| 72 | } |
| 73 | |
| 74 | WebKitFormSubmissionRequest* webkitFormSubmissionRequestCreate(const Vector<std::pair<String, String>>& values, Ref<WebFormSubmissionListenerProxy>&& listener) |
| 75 | { |
| 76 | WebKitFormSubmissionRequest* request = WEBKIT_FORM_SUBMISSION_REQUEST(g_object_new(WEBKIT_TYPE_FORM_SUBMISSION_REQUEST, nullptr)); |
| 77 | if (values.size()) { |
| 78 | request->priv->textFieldNames = adoptGRef(g_ptr_array_new_full(values.size(), g_free)); |
| 79 | request->priv->textFieldValues = adoptGRef(g_ptr_array_new_full(values.size(), g_free)); |
| 80 | for (size_t i = 0; i < values.size(); i++) { |
| 81 | g_ptr_array_add(request->priv->textFieldNames.get(), g_strdup(values[i].first.utf8().data())); |
| 82 | g_ptr_array_add(request->priv->textFieldValues.get(), g_strdup(values[i].second.utf8().data())); |
| 83 | } |
| 84 | } |
| 85 | request->priv->listener = WTFMove(listener); |
| 86 | return request; |
| 87 | } |
| 88 | |
| 89 | #if PLATFORM(GTK) |
| 90 | /** |
| 91 | * webkit_form_submission_request_get_text_fields: |
| 92 | * @request: a #WebKitFormSubmissionRequest |
| 93 | * |
| 94 | * Get a #GHashTable with the values of the text fields contained in the form |
| 95 | * associated to @request. Note that fields will be missing if the form |
| 96 | * contains multiple text input elements with the same name, so this |
| 97 | * function does not reliably return all text fields. |
| 98 | * |
| 99 | * Returns: (allow-none) (transfer none): a #GHashTable with the form |
| 100 | * text fields, or %NULL if the form doesn't contain text fields. |
| 101 | * |
| 102 | * Deprecated: 2.20. Use webkit_form_submission_request_list_text_fields() instead. |
| 103 | */ |
| 104 | GHashTable* webkit_form_submission_request_get_text_fields(WebKitFormSubmissionRequest* request) |
| 105 | { |
| 106 | g_return_val_if_fail(WEBKIT_IS_FORM_SUBMISSION_REQUEST(request), nullptr); |
| 107 | |
| 108 | if (!request->priv->values && request->priv->textFieldNames->len) { |
| 109 | request->priv->values = adoptGRef(g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free)); |
| 110 | for (unsigned i = 0; i < request->priv->textFieldNames->len; i++) { |
| 111 | GUniquePtr<char> name(g_strdup(static_cast<char*>(request->priv->textFieldNames->pdata[i]))); |
| 112 | GUniquePtr<char> value(g_strdup(static_cast<char*>(request->priv->textFieldValues->pdata[i]))); |
| 113 | g_hash_table_insert(request->priv->values.get(), name.release(), value.release()); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return request->priv->values.get(); |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | /** |
| 122 | * webkit_form_submission_request_list_text_fields: |
| 123 | * @request: a #WebKitFormSubmissionRequest |
| 124 | * @field_names: (out) (optional) (element-type utf8) (transfer none): |
| 125 | * names of the text fields in the form |
| 126 | * @field_values: (out) (optional) (element-type utf8) (transfer none): |
| 127 | * values of the text fields in the form |
| 128 | * |
| 129 | * Get lists with the names and values of the text fields contained in |
| 130 | * the form associated to @request. Note that names and values may be |
| 131 | * %NULL. |
| 132 | * |
| 133 | * If this function returns %FALSE, then both @field_names and |
| 134 | * @field_values will be empty. |
| 135 | * |
| 136 | * Returns: %TRUE if the form contains text fields, or %FALSE otherwise |
| 137 | * |
| 138 | * Since: 2.20 |
| 139 | */ |
| 140 | gboolean webkit_form_submission_request_list_text_fields(WebKitFormSubmissionRequest* request, GPtrArray** fieldNames, GPtrArray** fieldValues) |
| 141 | { |
| 142 | g_return_val_if_fail(WEBKIT_IS_FORM_SUBMISSION_REQUEST(request), FALSE); |
| 143 | |
| 144 | if (fieldNames) |
| 145 | *fieldNames = request->priv->textFieldNames.get(); |
| 146 | if (fieldValues) |
| 147 | *fieldValues = request->priv->textFieldValues.get(); |
| 148 | |
| 149 | return !!request->priv->textFieldNames->len; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * webkit_form_submission_request_submit: |
| 154 | * @request: a #WebKitFormSubmissionRequest |
| 155 | * |
| 156 | * Continue the form submission. |
| 157 | */ |
| 158 | void webkit_form_submission_request_submit(WebKitFormSubmissionRequest* request) |
| 159 | { |
| 160 | g_return_if_fail(WEBKIT_IS_FORM_SUBMISSION_REQUEST(request)); |
| 161 | |
| 162 | request->priv->listener->continueSubmission(); |
| 163 | request->priv->handledRequest = true; |
| 164 | } |
| 165 | |