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 "APIString.h" 24#include "ImmutableDictionary.h" 25#include "WebFormSubmissionListenerProxy.h" 26#include "WebKitFormSubmissionRequestPrivate.h" 27#include <wtf/gobject/GRefPtr.h> 28#include <wtf/text/CString.h> 29 30using namespace WebKit; 31 32/** 33 * SECTION: WebKitFormSubmissionRequest 34 * @Short_description: Represents a form submission request 35 * @Title: WebKitFormSubmissionRequest 36 * 37 * When a form is about to be submitted in a #WebKitWebView, the 38 * #WebKitWebView::submit-form signal is emitted. Its request argument 39 * contains information about the text fields of the form, that are 40 * typically used to store login information, returned in a 41 * #GHashTable by the webkit_form_submission_request_get_text_fields() 42 * method, and you can finally submit the form with 43 * webkit_form_submission_request_submit(). 44 * 45 */ 46 47struct _WebKitFormSubmissionRequestPrivate { 48 RefPtr<ImmutableDictionary> webValues; 49 RefPtr<WebFormSubmissionListenerProxy> listener; 50 GRefPtr<GHashTable> values; 51 bool handledRequest; 52}; 53 54WEBKIT_DEFINE_TYPE(WebKitFormSubmissionRequest, webkit_form_submission_request, G_TYPE_OBJECT) 55 56static void webkitFormSubmissionRequestDispose(GObject* object) 57{ 58 WebKitFormSubmissionRequest* request = WEBKIT_FORM_SUBMISSION_REQUEST(object); 59 60 // Make sure the request is always handled before finalizing. 61 if (!request->priv->handledRequest) 62 webkit_form_submission_request_submit(request); 63 64 G_OBJECT_CLASS(webkit_form_submission_request_parent_class)->dispose(object); 65} 66 67static void webkit_form_submission_request_class_init(WebKitFormSubmissionRequestClass* requestClass) 68{ 69 GObjectClass* objectClass = G_OBJECT_CLASS(requestClass); 70 objectClass->dispose = webkitFormSubmissionRequestDispose; 71} 72 73WebKitFormSubmissionRequest* webkitFormSubmissionRequestCreate(ImmutableDictionary* values, WebFormSubmissionListenerProxy* listener) 74{ 75 WebKitFormSubmissionRequest* request = WEBKIT_FORM_SUBMISSION_REQUEST(g_object_new(WEBKIT_TYPE_FORM_SUBMISSION_REQUEST, NULL)); 76 request->priv->webValues = values; 77 request->priv->listener = listener; 78 return request; 79} 80 81/** 82 * webkit_form_submission_request_get_text_fields: 83 * @request: a #WebKitFormSubmissionRequest 84 * 85 * Get a #GHashTable with the values of the text fields contained in the form 86 * associated to @request. 87 * 88 * Returns: (transfer none): a #GHashTable with the form text fields, or %NULL if the 89 * form doesn't contain text fields. 90 */ 91GHashTable* webkit_form_submission_request_get_text_fields(WebKitFormSubmissionRequest* request) 92{ 93 g_return_val_if_fail(WEBKIT_IS_FORM_SUBMISSION_REQUEST(request), 0); 94 95 if (request->priv->values) 96 return request->priv->values.get(); 97 98 if (!request->priv->webValues->size()) 99 return 0; 100 101 request->priv->values = adoptGRef(g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free)); 102 103 const ImmutableDictionary::MapType& map = request->priv->webValues->map(); 104 ImmutableDictionary::MapType::const_iterator end = map.end(); 105 for (ImmutableDictionary::MapType::const_iterator it = map.begin(); it != end; ++it) { 106 API::String* value = static_cast<API::String*>(it->value.get()); 107 g_hash_table_insert(request->priv->values.get(), g_strdup(it->key.utf8().data()), g_strdup(value->string().utf8().data())); 108 } 109 110 request->priv->webValues = 0; 111 112 return request->priv->values.get(); 113} 114 115/** 116 * webkit_form_submission_request_submit: 117 * @request: a #WebKitFormSubmissionRequest 118 * 119 * Continue the form submission. 120 */ 121void webkit_form_submission_request_submit(WebKitFormSubmissionRequest* request) 122{ 123 g_return_if_fail(WEBKIT_IS_FORM_SUBMISSION_REQUEST(request)); 124 125 request->priv->listener->continueSubmission(); 126 request->priv->handledRequest = true; 127} 128